DataSet.Tables[x].Rows.RemoteAt(y)

  • Thread starter Thread starter Timothy Parez
  • Start date Start date
T

Timothy Parez

Hi,

This is part of the code

DataTable y = new DataTable("newData");
int rowCount = destData.Tables[0].Rows.Count;

for (int i = (rowCount - 1);i >=0; i--)
{
DataRow newRow = destData.Tables[0].Rows;
destData.Tables[0].Rows.RemoveAt(i);
y.Rows.Add(newRow); //---------------> Throws an exception, saying the
row already belongs to another table.
}

Same result:

for (int i = (rowCount - 1);i >=0; i--)
{
DataRow newRow = destData.Tables[0].Rows;
destData.Tables[0].Rows.Remove(newRow);
y.Rows.Add(newRow);
}


How can I solve this
 
Timothy:

Since y is another table , why not use DataRow newRow = y.NewRow();

Then you could just set each column value of y's NewRow to
destData.Tables[0].Rows.

It looks like the probem is that since DataRows are reference types, newRow
the way it's coded now is referencing the same row as Tables[0].Rows. I
think you'll be ok if you effectively make a 'deep' copy of that row.

HTH,

Bill
 
Hi Timothy,

As usual have I the same idea as Bill about your problem, but as addition,
when it is a cloned datatable can you normaly also copy the itemarray
instead of itterating thru the items.

Cor
 
How about

DataTable y = destData.Tables[0].Copy();
y.TableName = "newData"; // If it's important for you that the table name
be "newData"
y.AcceptChanges();

That will copy both clone the schema and copy the data.

It will never work with the newrow on one table and sending it in another
since there is an internal reference to wich table the row belongs to.
Initialy the row will be in a "detached" state but still related to the
datatable that created it with the NewRow method.

Chris.
 
Back
Top