Moving DataRows between tables

N

Neil

Hi,

I have 2 datatables with the same schema. One table has
data in it and one is empty, I want to add all the rows
from table1 to table2 where the column iVolume is
positive. My code is below, when i execute this it tells
me that the row is already assigned to a table, how do i
get around this ir is there a better solution to my
problem?

Thanks
N

code...
DataTable dataTable = DS.Tables[gainTableName];
DataRow[] gainRows = dataTable.Select("iVolume > 0");
foreach ( DataRow gainRow in gainRows )
{
inputGainTable.Rows.Add(gainRow);
}
 
W

William Ryan

I think this will do it for you. I know there's not a
copy or clone method (at least I can't find it). You'll
still have a live reference to the row but I think in this
context it should work for you.



DataTable dataTable = DS.Tables[gainTableName];
DataRow[] gainRows = dataTable.Select("iVolume > 0");
foreach ( DataRow gainRow in gainRows )
{
DataRow d = new DataRow();
d = gainRow
inputGainTable.Rows.Add(d);
}

Good Luck,,

Bill
 
N

Neil

Hi,

Thanks for your help, unfortunately it did not work,
there is no overload for a datarow so instantiating a new
one is just..

DataRow myRow;

However I found the answer to my problem, FYI the code is
below, this will create a new row in memory and therefore
I could add it to my table

dt.Rows.Add(new Object[] { gainRow[0], gainRow[1] } );

Thanks
-----Original Message-----
I think this will do it for you. I know there's not a
copy or clone method (at least I can't find it). You'll
still have a live reference to the row but I think in this
context it should work for you.



DataTable dataTable = DS.Tables[gainTableName];
DataRow[] gainRows = dataTable.Select("iVolume > 0");
foreach ( DataRow gainRow in gainRows )
{
DataRow d = new DataRow();
d = gainRow
inputGainTable.Rows.Add(d);
}

Good Luck,,

Bill
-----Original Message-----
Hi,

I have 2 datatables with the same schema. One table has
data in it and one is empty, I want to add all the rows
from table1 to table2 where the column iVolume is
positive. My code is below, when i execute this it tells
me that the row is already assigned to a table, how do i
get around this ir is there a better solution to my
problem?

Thanks
N

code...
DataTable dataTable = DS.Tables[gainTableName];
DataRow[] gainRows = dataTable.Select("iVolume > 0");
foreach ( DataRow gainRow in gainRows )
{
inputGainTable.Rows.Add(gainRow);
}
.
.
 
M

Michael Lang

Try removing each DataRow from the first table before adding it to the
second.

DataRow dr = myDataTable.Rows[0]; //some selection criteria
myDataTable1.Remove(dr); //dr.RowState now is "Detached"
myDataTable2.Add(dr); //dr.RowState is "Added" ?? untested ??

As you can see this will affect the RowState. Removing a DataRow sets it
RowState to "Detached". I haven't checked but adding it to the other will
probably set it's RowState to "Added". This would make sense if you were to
update the second table to some data source as the InsertCommand would be
called. Since the DataRow was removed from the previous table instead of
calling the row's Delete() method, it will not be removed from the first
date source when updated from the first table (since there will be no row
for that record with RowState = Deleted)

We may be able to give better advice knowing your situation. Are there
actual data sources for these tables? Are you trying to merge multiple
databases into one? Or is all this in memory only?
 
K

Kawarjit Bedi

DataRow(s) from one table cannot be removed and then added to another table.
A DataRow Instance is always associated with the DataTable it was created
on - it's not a migratable instance.

You may want to take a look at the ImportRow API for importing rows from
other DataTable. After importing, the orginal row can be Removed from it's
DataTable, if needed.

Hope it helps.

Thanks,
Kawarjit [MSFT]

Michael Lang said:
Try removing each DataRow from the first table before adding it to the
second.

DataRow dr = myDataTable.Rows[0]; //some selection criteria
myDataTable1.Remove(dr); //dr.RowState now is "Detached"
myDataTable2.Add(dr); //dr.RowState is "Added" ?? untested ??

As you can see this will affect the RowState. Removing a DataRow sets it
RowState to "Detached". I haven't checked but adding it to the other will
probably set it's RowState to "Added". This would make sense if you were to
update the second table to some data source as the InsertCommand would be
called. Since the DataRow was removed from the previous table instead of
calling the row's Delete() method, it will not be removed from the first
date source when updated from the first table (since there will be no row
for that record with RowState = Deleted)

We may be able to give better advice knowing your situation. Are there
actual data sources for these tables? Are you trying to merge multiple
databases into one? Or is all this in memory only?

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/dbobjecter (code generator)
http://sourceforge.net/projects/genadonet ("generic" ADO.NET)

Neil said:
Hi,

I have 2 datatables with the same schema. One table has
data in it and one is empty, I want to add all the rows
from table1 to table2 where the column iVolume is
positive. My code is below, when i execute this it tells
me that the row is already assigned to a table, how do i
get around this ir is there a better solution to my
problem?

Thanks
N

code...
DataTable dataTable = DS.Tables[gainTableName];
DataRow[] gainRows = dataTable.Select("iVolume > 0");
foreach ( DataRow gainRow in gainRows )
{
inputGainTable.Rows.Add(gainRow);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top