|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
How to: Delete Rows in a DataTable
In order to retain the information that the dataset needs to send updates to the data source, use themethod to remove rows in a data table. For example, if your application uses a TableAdapter (or ), the adapter's Update method will delete rows in the database that have a of .
If your application does not need to send updates back to a data source, then it is possible to remove records by directly accessing the data row collection (). For information on locating a specific row in a , see .
To delete records from a data table
Call the Delete method of a.
This method does not physically remove the record; instead, it marks the record for deletion.
If you get the count property of a, the resulting count includes records that have been marked for deletion. To get an accurate count of only records that are not marked for deletion, you can loop through the collection looking at the RowState property of each record (records marked for deletion have a RowState of Deleted). Alternatively, you can create a data view of a dataset that filters based on row state and get the count property from there.
The following example shows how to call the Delete method to mark the first row in the Customers table as deleted:
For untyped datasets, use code similar to the following: