PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET How to copy DataRows between DataTables?

Reply

How to copy DataRows between DataTables?

 
Thread Tools Rating: Thread Rating: 2 votes, 2.50 average.
Old 30-06-2003, 10:38 AM   #1
Fabrício de Novaes Kucinskis
Guest
 
Posts: n/a
Default How to copy DataRows between DataTables?


Hi all,

I want to copy or move an existing DataRow wich belongs to a DataTable to
another DataTable, but I got the exception "This row already belongs to
another table".

I tried to use the ImportRow method, but this method inserts the row at the
end of the DataTable, and I need to put it at an specific position. The
method InsertAt returns the expeption ahead.

There's a way to copy a DataRow in a specific position? Or, maybe, a way to
detach the row from the original DataTable?
Thanks in advance,


Fabrício de Novaes Kucinskis.


  Reply With Quote
Old 30-06-2003, 01:13 PM   #2
Fabrício de Novaes Kucinskis
Guest
 
Posts: n/a
Default Re: How to copy DataRows between DataTables?

Well, I found a solution, copying the source row data to a new row, using
the ItemArray property, and then attaching the new row to the destination
table via InsertAt.

The problem now is that the row is attached to the end of the DataTable, not
in the position I set in InsertAt. My DataTable has a binded DataGrid.

I search the web, and it appears to be a .NET bug. Is it correct? If no,
what can I do??? If yes, WHAT CAN I DO?????
Thanks again,


Fabrício.



"Fabrício de Novaes Kucinskis" <fabricio.nk@uol.com.br> wrote in message
news:eF3NfRvPDHA.2480@tk2msftngp13.phx.gbl...
> Hi all,
>
> I want to copy or move an existing DataRow wich belongs to a DataTable to
> another DataTable, but I got the exception "This row already belongs to
> another table".
>
> I tried to use the ImportRow method, but this method inserts the row at

the
> end of the DataTable, and I need to put it at an specific position. The
> method InsertAt returns the expeption ahead.
>
> There's a way to copy a DataRow in a specific position? Or, maybe, a way

to
> detach the row from the original DataTable?
> Thanks in advance,
>
>
> Fabrício de Novaes Kucinskis.
>
>



  Reply With Quote
Old 30-06-2003, 09:45 PM   #3
David Brenchley
Guest
 
Posts: n/a
Default Re: How to copy DataRows between DataTables?

I am unsure if this is a bug or not. How you may get
around this is use a sorted DataView on the DataTable and
bind your grid to the DataView.

>-----Original Message-----
>Well, I found a solution, copying the source row data to

a new row, using
>the ItemArray property, and then attaching the new row

to the destination
>table via InsertAt.
>
>The problem now is that the row is attached to the end

of the DataTable, not
>in the position I set in InsertAt. My DataTable has a

binded DataGrid.
>
>I search the web, and it appears to be a .NET bug. Is it

correct? If no,
>what can I do??? If yes, WHAT CAN I DO?????
>Thanks again,
>
>
>Fabrício.
>
>
>
>"Fabrício de Novaes Kucinskis" <fabricio.nk@uol.com.br>

wrote in message
>news:eF3NfRvPDHA.2480@tk2msftngp13.phx.gbl...
>> Hi all,
>>
>> I want to copy or move an existing DataRow wich

belongs to a DataTable to
>> another DataTable, but I got the exception "This row

already belongs to
>> another table".
>>
>> I tried to use the ImportRow method, but this method

inserts the row at
>the
>> end of the DataTable, and I need to put it at an

specific position. The
>> method InsertAt returns the expeption ahead.
>>
>> There's a way to copy a DataRow in a specific

position? Or, maybe, a way
>to
>> detach the row from the original DataTable?
>> Thanks in advance,
>>
>>
>> Fabrício de Novaes Kucinskis.
>>
>>

>
>
>.
>

  Reply With Quote
Old 01-07-2003, 03:29 AM   #4
Fabrício de Novaes Kucinskis
Guest
 
Posts: n/a
Default Re: How to copy DataRows between DataTables?

Hi, David. Thanks for your answer.

I cannot use a DataView because the user wants to edit the data in DataGrid.




"David Brenchley" <brenchld@iomega.com> wrote in message
news:052e01c33f50$e8771e00$a101280a@phx.gbl...
I am unsure if this is a bug or not. How you may get
around this is use a sorted DataView on the DataTable and
bind your grid to the DataView.

>-----Original Message-----
>Well, I found a solution, copying the source row data to

a new row, using
>the ItemArray property, and then attaching the new row

to the destination
>table via InsertAt.
>
>The problem now is that the row is attached to the end

of the DataTable, not
>in the position I set in InsertAt. My DataTable has a

binded DataGrid.
>
>I search the web, and it appears to be a .NET bug. Is it

correct? If no,
>what can I do??? If yes, WHAT CAN I DO?????
>Thanks again,
>
>
>Fabrício.
>
>
>
>"Fabrício de Novaes Kucinskis" <fabricio.nk@uol.com.br>

wrote in message
>news:eF3NfRvPDHA.2480@tk2msftngp13.phx.gbl...
>> Hi all,
>>
>> I want to copy or move an existing DataRow wich

belongs to a DataTable to
>> another DataTable, but I got the exception "This row

already belongs to
>> another table".
>>
>> I tried to use the ImportRow method, but this method

inserts the row at
>the
>> end of the DataTable, and I need to put it at an

specific position. The
>> method InsertAt returns the expeption ahead.
>>
>> There's a way to copy a DataRow in a specific

position? Or, maybe, a way
>to
>> detach the row from the original DataTable?
>> Thanks in advance,
>>
>>
>> Fabrício de Novaes Kucinskis.
>>
>>

>
>
>.
>



  Reply With Quote
Old 03-07-2003, 05:54 AM   #5
Marc Scheuner [MVP ADSI]
Guest
 
Posts: n/a
Default Re: How to copy DataRows between DataTables?

>I cannot use a DataView because the user wants to edit the data in DataGrid.

And why do you think you can't edit your data anymore, if you use a
DataView?? You still can!

Check it out - every DataTable has a "DefaultView" property, which
exposes the default view of the table - you can use that to set the
Sort property of the view, and then use the view as your grid's
source, instead of your table directly.

Read up on ADO.NET in a good book - it has *LOTS* of goodies to offer!
DataView also allow you to specify e.g. a RowFIlter - very powerful
stuff. And everything stays editable!

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
  Reply With Quote
Old 03-07-2003, 11:30 AM   #6
Fabrício de Novaes Kucinskis
Guest
 
Posts: n/a
Default Re: How to copy DataRows between DataTables?

Oh my God. I don't believe I lost a lot of time, based on this supposition.

Marc, thanks for your answer. I'll try your tips.


"Marc Scheuner [MVP ADSI]" <m.scheuner@inova.SPAMBEGONE.ch> wrote in message
news:c5h7gvkjkoq9ap4m9t3pdr1h41gbr9bp0h@4ax.com...
> >I cannot use a DataView because the user wants to edit the data in

DataGrid.
>
> And why do you think you can't edit your data anymore, if you use a
> DataView?? You still can!
>
> Check it out - every DataTable has a "DefaultView" property, which
> exposes the default view of the table - you can use that to set the
> Sort property of the view, and then use the view as your grid's
> source, instead of your table directly.
>
> Read up on ADO.NET in a good book - it has *LOTS* of goodies to offer!
> DataView also allow you to specify e.g. a RowFIlter - very powerful
> stuff. And everything stays editable!
>
> Marc
> ================================================================
> Marc Scheuner May The Source Be With You!
> Bern, Switzerland m.scheuner(at)inova.ch



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off