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
---
On Aug 28, 11:12 pm, Ana <[email protected]> wrote:
> 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
>
> On Aug 27, 12:19 am, Cerebrus <[email protected]> wrote:
>
>
>
> > 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.
>
> > On Aug 27, 2:24 am, Ana <[email protected]> wrote:
>
> > > 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- Hide quoted text -
>
> - Show quoted text -