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)?

Re: Remove DataTable Entries by Derek

Derek
Wed Jul 30 13:51:39 CDT 2003

This is a multi-part message in MIME format.

------=_NextPart_000_0096_01C356A1.B33DA1B0
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

Hi Michael. Also, DataTable.Select( string) should work...
[But I'm not sure if it works on just the primary key field or all =
columns in the data row because it returns an array of datarows...]

Derek LaZard

"Michael Lang" <m@l.com> wrote in message =
news:eS3USWfVDHA.2368@TK2MSFTNGP09.phx.gbl...
> 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.
>=20
> 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?
>=20
> 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.
>=20
> DataTable dtA =3D ...;
> DataTable dtB =3D ...;
> for (int i=3D0; i < dtA.Count; i++)
> {
> DataRow drA =3D dtA.Rows[i];
> object pkA =3D ... primary key value in drA ...
> DataRow drFound =3D dtB.Rows.Find(pkA);
> if (drFound !=3D null)
> {
> drFound.Delete();
> }
> }
>=20
> 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.
>=20
> Michael Lang
>=20
> "frank" <frank@frank.com> wrote in message
> news:ey$jvPdVDHA.1692@TK2MSFTNGP11.phx.gbl...
> > 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)?
> >
> >
>=20
>
------=_NextPart_000_0096_01C356A1.B33DA1B0
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<META content=3D"MSHTML 6.00.2800.1106" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT size=3D2>Hi Michael.&nbsp; Also, =
DataTable.<STRONG>Select</STRONG>(=20
string) should work...</FONT></DIV>
<DIV><FONT size=3D2>[But I'm not sure if it works on just the primary =
key field or=20
all columns in the data row because it returns an array of=20
datarows...]</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Derek LaZard</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>"Michael Lang" &lt;</FONT><A =
href=3D"mailto:m@l.com"><FONT=20
size=3D2>m@l.com</FONT></A><FONT size=3D2>&gt; wrote in message =
</FONT><A=20
href=3D"news:eS3USWfVDHA.2368@TK2MSFTNGP09.phx.gbl"><FONT=20
size=3D2>news:eS3USWfVDHA.2368@TK2MSFTNGP09.phx.gbl</FONT></A><FONT=20
size=3D2>...</FONT></DIV><FONT size=3D2>&gt; To delete a row from a =
DataTable, just=20
call the "Delete" method on the<BR>&gt; DataRow you want to =
delete.&nbsp; Do NOT=20
call "DataTable.Rows.Remove(datarow<BR>&gt; dr)".&nbsp; The Remove =
method of the=20
DataRowCollection only removes it from the<BR>&gt; collection, which =
will not be=20
queued for deletion from the database.<BR>&gt; <BR>&gt; As for how you =
determine=20
what to delete?&nbsp; As far as I know, there is no<BR>&gt; automated =
way to=20
determine which records have the same field values (PK or<BR>&gt; =
otherwise) in=20
two tables?<BR>&gt; <BR>&gt; My intial idea would be to loop through one =
table,=20
and for each item check<BR>&gt; for an item of the same PK value in the =
second=20
table.&nbsp; If one exists, call<BR>&gt; the Delete method on it as I =
noted=20
above.<BR>&gt; <BR>&gt; DataTable dtA =3D ...;<BR>&gt; DataTable dtB =3D =

...;<BR>&gt; for (int i=3D0; i &lt; dtA.Count; i++)<BR>&gt; {<BR>&gt;=20
&nbsp;&nbsp;&nbsp; DataRow drA =3D dtA.Rows[i];<BR>&gt; =
&nbsp;&nbsp;&nbsp; object=20
pkA =3D ... primary key value in drA ...<BR>&gt; &nbsp;&nbsp;&nbsp; =
DataRow=20
drFound =3D dtB.Rows.Find(pkA);<BR>&gt; &nbsp;&nbsp;&nbsp; if (drFound =
!=3D=20
null)<BR>&gt; &nbsp;&nbsp;&nbsp; {<BR>&gt;=20
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; drFound.Delete();<BR>&gt;=20
&nbsp;&nbsp;&nbsp; }<BR>&gt; }<BR>&gt; <BR>&gt; I haven't run this code, =
I just=20
made it up from looking at the help.&nbsp; In<BR>&gt; theory, it should=20
work.&nbsp; For details look at the help on DataTable,<BR>&gt;=20
DataRowCollection, and DataRow.<BR>&gt; <BR>&gt; Michael Lang<BR>&gt; =
<BR>&gt;=20
"frank" &lt;</FONT><A href=3D"mailto:frank@frank.com"><FONT=20
size=3D2>frank@frank.com</FONT></A><FONT size=3D2>&gt; wrote in =
message<BR>&gt;=20
</FONT><A href=3D"news:ey$jvPdVDHA.1692@TK2MSFTNGP11.phx.gbl"><FONT=20
size=3D2>news:ey$jvPdVDHA.1692@TK2MSFTNGP11.phx.gbl</FONT></A><FONT=20
size=3D2>...<BR>&gt; &gt; I have two DataTables, A and B. Both have the =
primary=20
key "CID". How do I<BR>&gt; &gt; remove all entries in Table A from =
Table B=20
(with the same PK)?<BR>&gt; &gt;<BR>&gt; &gt;<BR>&gt; <BR>&gt;=20
</FONT></BODY></HTML>

------=_NextPart_000_0096_01C356A1.B33DA1B0--