Re: Add array of rows to DataTable

S

S. Justin Gengo

Frank,

I don't know of any way to add an array of rows. I believe that you'll have
to iterate through the array and add each one at a time:

DataTable.Rows.Add(SingleDataRow)

But to clear all rows is pretty easy:

DataTable.Rows.Clear()

Justin
 
F

frank

S. Justin Gengo said:
frank said:
How do I add an array of DataRows (i.e. DataRow[]) to a DataTable?

Also, how do I remove all rows from a DataTable?
Frank,

I don't know of any way to add an array of rows. I believe that you'll have
to iterate through the array and add each one at a time:

DataTable.Rows.Add(SingleDataRow)

OK, I've done that but now it says "This row already belongs to another
table". I know you can't move a DataRow from one collection to another if it
belongs to another - so I tried dtFiltered.ImportRow(dr).

When I do it that way, it appears to add the rows without any problem, but
when it comes to actually referencing and using the data later on.

If I try:

Console.Write(dr[0].ToString() + "\r\n")

It says "Cannot find column 0".

Any ideas?
But to clear all rows is pretty easy:

DataTable.Rows.Clear()

I tried this to see if it would let me use the Add method, but it didn't
work.
 
S

S. Justin Gengo

Frank,

If you're referring to the row that was just added to the table then the row
object might get cleared when it's imported. That's my best guess.

Can you refer to it in full? (i.e. DataTableImportedTo.Rows(0)(0).ToString)

Sorry, but I don't know the equivilant in C#. It should be pretty close to
the same though...

Justin


frank said:
S. Justin Gengo said:
frank said:
How do I add an array of DataRows (i.e. DataRow[]) to a DataTable?

Also, how do I remove all rows from a DataTable?
Frank,

I don't know of any way to add an array of rows. I believe that you'll have
to iterate through the array and add each one at a time:

DataTable.Rows.Add(SingleDataRow)

OK, I've done that but now it says "This row already belongs to another
table". I know you can't move a DataRow from one collection to another if it
belongs to another - so I tried dtFiltered.ImportRow(dr).

When I do it that way, it appears to add the rows without any problem, but
when it comes to actually referencing and using the data later on.

If I try:

Console.Write(dr[0].ToString() + "\r\n")

It says "Cannot find column 0".

Any ideas?
But to clear all rows is pretty easy:

DataTable.Rows.Clear()

I tried this to see if it would let me use the Add method, but it didn't
work.
 
F

frank

S. Justin Gengo said:
frank said:
S. Justin Gengo said:
How do I add an array of DataRows (i.e. DataRow[]) to a DataTable?

Also, how do I remove all rows from a DataTable?


Frank,

I don't know of any way to add an array of rows. I believe that
you'll
have
to iterate through the array and add each one at a time:

DataTable.Rows.Add(SingleDataRow)

OK, I've done that but now it says "This row already belongs to another
table". I know you can't move a DataRow from one collection to another
if
it
belongs to another - so I tried dtFiltered.ImportRow(dr).

When I do it that way, it appears to add the rows without any problem, but
when it comes to actually referencing and using the data later on.

If I try:

Console.Write(dr[0].ToString() + "\r\n")

It says "Cannot find column 0".

Any ideas?
But to clear all rows is pretty easy:

DataTable.Rows.Clear()

I tried this to see if it would let me use the Add method, but it didn't
work.

If you're referring to the row that was just added to the table then the row
object might get cleared when it's imported. That's my best guess.

Can you refer to it in full? (i.e. DataTableImportedTo.Rows(0)(0).ToString)

Sorry, but I don't know the equivilant in C#. It should be pretty close to
the same though...

Thanks for the help, but I found and solved the problem.

For archiving purposes, here's what it was:

When I used the ImportRow method it doesn't actually create any DataColumns
in the DataTable and as DataColumns are required to reference the data (even
though it *was* there) I could actually access it.

I resolved the issue by creating the DataColumns first, before using
ImportRow.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top