Delete
DataRow.Delete();
If the RowState
of the DataRow
is Added
, the row is removed from the table.
Otherwise, the RowState
of the
DataRow
is set to Deleted
,
effectively marking it for deletion.
Three examples follow that demonstrate how to use the
Delete( )
method and that highlight the effect of
the RowState
property and of the
AcceptChanges( )
and RejectChanges( )
methods. The first example shows the relationship between
the Delete( )
method and the
AcceptChanges( )
method:
// delete the first row from the table DataRow row = dt.Rows[0]; row.Delete(); // RowState changed to Deleted dt.AcceptChanges(); // row is removed from the DataTable
The next example shows the relationship between the Delete( )
method and the RejectChanges( )
method:
// delete the first row from the table DataRow row = dt.Rows[0]; row.Delete(); // RowState changed to Deleted dt.RejectChanges(); // RowState reverts to Unchanged
Finally, the following example shows the relationship between newly
added rows and the Delete( )
method:
// Create a new row row = dt.NewRow(); // ... code to set the data for the row // add the row to the table dt.Rows.Add(row); // RowState is Added // delete the newly added row row.Delete(); // row is removed from the table