To delete a row from a DataTable, just call the "Delete" method on the
DataRow you want to delete. Do NOT call "DataTable.Rows.Remove(datarow
dr)". The Remove method of the DataRowCollection only removes it from the
collection, which will not be queued for deletion from the database.
As for how you determine what to delete? As far as I know, there is no
automated way to determine which records have the same field values (PK or
otherwise) in two tables?
My intial idea would be to loop through one table, and for each item check
for an item of the same PK value in the second table. If one exists, call
the Delete method on it as I noted above.
DataTable dtA = ...;
DataTable dtB = ...;
for (int i=0; i < dtA.Count; i++)
{
DataRow drA = dtA.Rows
;
object pkA = ... primary key value in drA ...
DataRow drFound = dtB.Rows.Find(pkA);
if (drFound != null)
{
drFound.Delete();
}
}
I haven't run this code, I just made it up from looking at the help. In
theory, it should work. For details look at the help on DataTable,
DataRowCollection, and DataRow.
Michael Lang
frank said:
I have two DataTables, A and B. Both have the primary key "CID". How do I
remove all entries in Table A from Table B (with the same PK)?