Copy a DataRow from one DataTable to another

  • Thread starter Thread starter Shawn
  • Start date Start date
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
 
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...
 
Yes, but what do I have to do to get the new DataRow into index 5 in the
DataTable?

Shawn
 
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
 
Back
Top