Problem inserting a row in a DataTable

478 views
Skip to first unread message

Ana

unread,
Aug 26, 2009, 5:15:46 PM8/26/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,

I have two DataTables (dTable and childrenTable) and I need to insert
one row from childrenTable to dTable in a specific position. My code
is like this:

For Each row In childrenTable.Rows
Dim newRow As DataRow = row
dTable.Rows.InsertAt(newRow, indexInsert)
indexInsert = indexInsert + 1
Next

However, I'm having the error: "This row already belongs to another
table". Any ideas about how to solve this problem?

Thanks in advance,

Ana

Ana

unread,
Aug 26, 2009, 5:24:29 PM8/26/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Never mind. I added the values for each column and now it works.

For Each row In childrenTable.Rows
Dim newRow As DataRow = dTable.NewRow
newRow("Id") = row("Id")
newRow("Name") = row("Name")
dTable.Rows.InsertAt(newRow, indexInsert)
indexInsert = indexInsert + 1
Next

Cerebrus

unread,
Aug 27, 2009, 2:19:56 AM8/27/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
This is not the correct way. What you are doing in this method is to
create a new Row instance and populate its values, instead of
inserting the existing DataRow instance from the child table. Your
code will become unmanageable if the no. of columns increases.

IMO, the correct way to do this would probably be to use the
DataTable.ImportRow() method (and then the DataRowCollection.InsertAt
() method). I'm just relying on memory here, so I may be mistaken
about the exact implementation.

Ana

unread,
Aug 28, 2009, 2:12:44 PM8/28/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I thought of using the ImportRow() method, but I gave up of the idea
because I needed to insert the Row in a specific position in the
Table. How can I do this using the ImportRow()?

Thanks,

Ana

Cerebrus

unread,
Aug 29, 2009, 7:21:36 AM8/29/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hmmm... I found a little time to check it out and it turns out that I
was mistaken. Contrary to my initial assumption, ImportRow() does
actually *insert* the DataRow at the last index position in the Table.
I was probably confusing it with the way that the
XmlDocument.ImportNode() works.

So, the solution in your case will have to involve creation of a new
DataRow. What can be optimized however is the copying of individual
DataColumns. Here's a sample function I wrote to do this:

---
Private Sub ImportRows(ByVal srcTable As DataTable, ByVal tgtTable As
DataTable, ByVal insertPos As Integer)
For Each srcDR As DataRow In srcTable.Rows
Dim tgtDR As DataRow = tgtTable.NewRow()
tgtDR.ItemArray = srcDR.ItemArray
tgtTable.Rows.InsertAt(tgtDR, insertPos)
insertPos += 1
Next
End Sub
---
> > >                 Next- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages