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();
二维数组转换成为DataTable
最新推荐文章于 2024-01-09 16:06:43 发布