Populate a DataTable with an Array

I have an array like this:

const int dim = 1000; double[,] array = new double[dim, dim]; Random ran = new Random(); for (int r = 0; r < dim; r++) for (int c = 0; c < dim; c++) array[r, c] = (ran.Next(dim)); DataTable dataTable = new DataTable(); 

Is it possible to populate dataTable with array data?

+6
arrays c # datatable
source share
3 answers

Try something like this:

 var dt = new DataTable(); //AddColumns for (int c = 0; c < dim; c++) dt.Columns.Add(c.ToString(), typeof(double)); //LoadData for (int r = 0; r < dim; r++) dt.LoadDataRow(arry[r]); 
+5
source share

You need to set up the columns and then load one row at a time using

DataTable.LoadDataRow() , which takes an object[]

See an example on the MSDN page.

+1
source share

Yes you can, but first you have to add 1000 columns to the table as follows:

 dataTable.Columns.Add("Column" + c, typeof(double)); 

However, the real question is why you want to do this. DataTable is a very inefficient data structure.

0
source share

All Articles