Two dimensional array into datatable

G

Guest

Hi,

I would like to know how fill a datatable from a two dimensional array.

Any advice will be grateful.
 
G

Guest

Hi Josema,

Is your size of array constant, or any of it's length?

If HEIGHT is constant:

object[,] yourArray;
DataRowCollection yourRows; // your DataTable's rows (eg. ID, VALUE)

int newId;
object[] itemArray=new object[2];
for(y=0; y<HEIGHT; y++) {
for(x=0; x<width; x++) {
newId=(y*HEIGHT)+x;
itemArray[0]=newId;
itemArray[1]=yourArray[y,x];
yourRows.Add(itemArray);
}
}

HTH
Marcin

PS: The Simplest, You can save values with (X, Y) as a DataTable
indices.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

AFAIK there is no way of doing that, the only way to go is iterate and
create/insert the items in the datatable

cheers,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi again,


You do not have to iterate in the second dimension ( the columns of the
row) , you could use the overloaded version of DataTable.Rows.Add(
objects[] )


cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top