Copy a DataRow from one DataTable to another

S

Shawn

Hi. I have 2 DataTables, dataTable1 and dataTable2. dataTable1 contains a
DataRow that I need to insert at index 5 in dataTable2. Here is my code:

For Each dataRow1 In dataTable1.Rows
If dataRow1.Item("id") = "3353" Then
dataRow2 = dataRow1
End If
Next


dataTable2.Rows.InsertAt(dataRow2, 5)

The line above gives me this error: System.ArgumentException: This row
already belongs to another table.
What is the right way to do this?

Thanks,
Shawn
 
C

Craig Deelsnyder

Shawn said:
Hi. I have 2 DataTables, dataTable1 and dataTable2. dataTable1 contains a
DataRow that I need to insert at index 5 in dataTable2. Here is my code:

For Each dataRow1 In dataTable1.Rows
If dataRow1.Item("id") = "3353" Then
dataRow2 = dataRow1
End If
Next


dataTable2.Rows.InsertAt(dataRow2, 5)

The line above gives me this error: System.ArgumentException: This row
already belongs to another table.
What is the right way to do this?

Thanks,
Shawn

Try dataTable2.ImportRow method instead...
 
S

Shawn

Yes, but what do I have to do to get the new DataRow into index 5 in the
DataTable?

Shawn
 
R

Robbe Morris [C# MVP]

Why bother? You can use the .Select method to return an array of DataRows
in any order you want. At the very least, you could add a dummy column to
your
table at runtime as a column to sort on and populate based on your business
logic.

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx



Shawn said:
Yes, but what do I have to do to get the new DataRow into index 5 in the
DataTable?

Shawn
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top