How to copy DataRows between DataTables?

  • Thread starter Thread starter Fabrício de Novaes Kucinskis
  • Start date Start date
F

Fabrício de Novaes Kucinskis

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.
 
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.
 
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.
 
Hi, David. Thanks for your answer.

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




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