Share via


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 the Delete method to remove rows in a data table. For example, if your application uses a TableAdapter (or DataAdapter), the adapter's Update method will delete rows in the database that have a RowState of Deleted.

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 (Remove). For information on locating a specific row in a DataTable, see How to: Locate a Specific Row in a DataTable.

To delete records from a data table

  • Call the Delete method of a DataRow.

    This method does not physically remove the record; instead, it marks the record for deletion.

    Note

    If you get the count property of a DataRowCollection, 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:

    NorthwindDataSet1.Customers.Rows(0).Delete()
    
    northwindDataSet1.Customers.Rows[0].Delete();
    
  • For untyped datasets, use code similar to the following:

    DataSet1.Tables("Customers").Rows(0).Delete()
    
    dataSet1.Tables["Customers"].Rows[0].Delete();
    

See Also

Tasks

How to: Add Rows to a DataTable

How to: Edit Rows in a DataTable

How to: Locate a Specific Row in a DataTable

Concepts

Preparing Your Application to Receive Data

Fetching Data into Your Application

Binding Controls to Data in Visual Studio

Editing Data in Your Application

Validating Data

Saving Data