|
//GridViewのAutoGenerateColumnsをTrueに設定すること
DataTable dt = new DataTable("name");
DataSet ds = new DataSet();
//列の見出しの設定
string cString = "AAA,BBB,CCC,DDD";//とりあえず4列
string[] bs = cString.Split(',');
for (int i = 0; i < bs.Length; i++)
{
dt.Columns.Add(bs[i]);
}
ds.Tables.Add(dt); //見出し追加
//行を10行追加
for(int i=0;i<10;i++)
{
DataRow drow = ds.Tables["name"].NewRow();
for (int j = 0; j < bs.Length ; j++)
{
try
{
drow[dt.Columns[j].Caption] = (i+1).ToString() + "行:"
+ (j+1).ToString() + "列";
}
catch
{ }
}
ds.Tables["name"].Rows.Add(drow);
}
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.DataMember = ds.Tables[0].TableName;