Monday, March 12, 2012

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.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.
>
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=EDcio.
>
>"Fabr=EDcio 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=EDcio de Novaes Kucinskis.
>>
>
>.
>
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.
>>
>
>.
>
>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.
"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

Labels: , , , , , , , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home