ConvertToDataTable方法就是轉化方法,參數爲一個二維數組
public static DataTable ConvertToDataTable(string[,] arr)
{
DataTable dataSouce = new DataTable();
for (int i = 0; i < arr.GetLength(1); i++)
{
DataColumn newColumn = new DataColumn(GetCode(i), arr[0, 0].GetType());
dataSouce.Columns.Add(newColumn);
}
for (int i = 0; i < arr.GetLength(0); i++)
{
DataRow newRow = dataSouce.NewRow();
for (int j = 0; j < arr.GetLength(1); j++)
{
newRow[GetCode(j)] = arr[i, j];
}
dataSouce.Rows.Add(newRow);
}
return dataSouce;
}
接着綁定到GridView中去
DataTable newdt = ConvertToDataTable(myArea);
this.grid.DataSource = newdt;
this.grid.DataBind();