Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Copy a DataRow from one DataTable to another

Reply
Thread Tools

Copy a DataRow from one DataTable to another

 
 
Shawn
Guest
Posts: n/a
 
      03-09-2005
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


 
Reply With Quote
 
 
 
 
Craig Deelsnyder
Guest
Posts: n/a
 
      03-09-2005
Shawn wrote:
> 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...

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
 
Reply With Quote
 
 
 
 
Shawn
Guest
Posts: n/a
 
      03-09-2005
Yes, but what do I have to do to get the new DataRow into index 5 in the
DataTable?

Shawn


"Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
news:#(E-Mail Removed)...
> Shawn wrote:
> > 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...
>
> --
> Craig Deelsnyder
> Microsoft MVP - ASP/ASP.NET



 
Reply With Quote
 
Robbe Morris [C# MVP]
Guest
Posts: n/a
 
      03-09-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Yes, but what do I have to do to get the new DataRow into index 5 in the
> DataTable?
>
> Shawn
>
>
> "Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
> news:#(E-Mail Removed)...
>> Shawn wrote:
>> > 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...
>>
>> --
>> Craig Deelsnyder
>> Microsoft MVP - ASP/ASP.NET

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I create a copy of a DataRow that lasts after datatable is cleared? Bennett Haselton ASP .Net 0 11-11-2004 09:35 AM
How can I add a row from a typed datatable to another instance of that typed datatable? Ersin Gençtürk ASP .Net 1 10-06-2004 01:11 PM
Getting a Field in a DataSet, DataTable or DataRow, Rob G ASP .Net 0 06-07-2004 02:59 PM
How to copy datarow from one datatable to another ? ypul ASP .Net 3 04-29-2004 10:08 AM
Adding DataTable Rows To another DataTable J. Babe ASP .Net 1 08-15-2003 05:04 PM



Advertisments