天天看點

二維數組轉換成DataTable

public static DataTable ToDataTable(string[,] arr)
        {
                DataTable dataSouce = new DataTable();     
                for (int i = 0; i < arr.GetLength(1); i++)     
                {     
                      DataColumn newColumn = new DataColumn(i.ToString(), 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[j.ToString()] = arr[i, j];     
                     }     
                     dataSouce.Rows.Add(newRow);     
                }  
                return dataSouce;  
        }