Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

How to delete a row in the DataTable (solution)

The VS .NET / ADO .NET documentation is very poor in the parts about deleting rows from the table. It should tell you the following, but it doesn't.

Let's say you want to delete a record. You read the help, and you write something like:

row.Delete();
table.AcceptChanges();

or, you write something like:

table.Rows.RemoveAt(i);

which is equivalent to the code above.


All is good and well. You have deleted the row from the data table. Now, you want to update the physical database, and you call the Update method.

BZZZZZZZT! Wrong answer! The database rows won't be deleted at all!


What you need to do (and the MS documentation doesn't tell you - just check in the parts about RemoveAt, Delete, Update - it is nowhere to be found) is:

row.Delete();  // mark row for deletion

...Update();  // update the physical database

table.AcceptChanges(); // remove the rows marked for deletion from the dataset


Microsoft, please update your documentation! You could easily put this in the help pages for Delete(), RemoveAt(), or Update()!

Thank you very much!

Sugar Ray
Tuesday, July 15, 2003

You don't need to do the table.AcceptChanges() at the end as the DataAdapter.Update(table) does this already.

I guess the logical reason for not mentioning the DataAdapter.Update stuff in the documentation for DataTable is that the DataTable is completely independent of the DataAdapter. But yes - a link to an overview topic would be sensible there.

Rather than looking at the lower-level API documentation try stepping back from the trees/wood a bit and check out the overview stuff in the "Programming with the .NET Framework" section of the SDK/MSDN.

Duncan Smart
Tuesday, July 15, 2003

*  Recent Topics

*  Fog Creek Home