DataTable Edits

When you make changes to column values in a DataRow1, the changes are immediately placed in the current state of the row. The DataRowState2 is then set to Modified, and the changes are accepted or rejected using the AcceptChanges3 or RejectChanges4 methods of the DataRow. The DataRow also provides three methods that you can use to suspend the state of the row while you are editing it. These methods are BeginEdit5, EndEdit6, and CancelEdit7.

When you modify column values in a DataRow directly, the DataRow manages the column values using the Current, Default, and Original row versions. In addition to these row versions, the BeginEdit, EndEdit, and CancelEdit methods use a fourth row version: Proposed. For more information about row versions, see Row States and Row Versions8.

The Proposed row version exists during an edit operation that begins by calling BeginEdit and that ends either by using EndEdit or CancelEdit, or by calling AcceptChanges or RejectChanges.

During the edit operation, you can apply validation logic to individual columns by evaluating the ProposedValue in the ColumnChanged event of the DataTable. The ColumnChanged event holds DataColumnChangeEventArgs that keep a reference to the column that is changing and to the ProposedValue. After you evaluate the proposed value, you can either modify it or cancel the edit. When the edit is ended, the row moves out of the Proposed state.

You can confirm edits by calling EndEdit, or you can cancel them by calling CancelEdit. Note that while EndEdit does confirm your edits, the DataSet does not actually accept the changes until AcceptChanges is called. Note also that if you call AcceptChanges before you have ended the edit with EndEdit or CancelEdit, the edit is ended and the Proposed row values are accepted for both the Current and Original row versions. In the same manner, calling RejectChanges ends the edit and discards the Current and Proposed row versions. Calling EndEdit or CancelEdit after calling AcceptChanges or RejectChanges has no effect because the edit has already ended.

The following example demonstrates how to use BeginEdit with EndEdit and CancelEdit. The example also checks the ProposedValue in the ColumnChanged event and decides whether to cancel the edit.

C#
DataTable workTable  = new DataTable();
workTable.Columns.Add("LastName", typeof(String));

workTable.ColumnChanged += 
  new DataColumnChangeEventHandler(OnColumnChanged);

DataRow workRow = workTable.NewRow();
workRow[0] = "Smith";
workTable.Rows.Add(workRow);

workRow.BeginEdit();
// Causes the ColumnChanged event to write a message and cancel the edit.
workRow[0] = "";     
workRow.EndEdit();

// Displays "Smith, New".
Console.WriteLine("{0}, {1}", workRow[0], workRow.RowState);  

protected static void OnColumnChanged(
  Object sender, DataColumnChangeEventArgs args)
{
  if (args.Column.ColumnName == "LastName")
    if (args.ProposedValue.ToString() == "")
    {
      Console.WriteLine("Last Name cannot be blank. Edit canceled.");
      args.Row.CancelEdit();
    }
}
Links Table
1http://msdn.microsoft.com/en-us/library/system.data.datarow.aspx
2http://msdn.microsoft.com/en-us/library/system.data.datarowstate.aspx
3http://msdn.microsoft.com/en-us/library/system.data.datarow.acceptchanges.aspx
4http://msdn.microsoft.com/en-us/library/system.data.datarow.rejectchanges.aspx
5http://msdn.microsoft.com/en-us/library/system.data.datarow.beginedit.aspx
6http://msdn.microsoft.com/en-us/library/system.data.datarow.endedit.aspx
7http://msdn.microsoft.com/en-us/library/system.data.datarow.canceledit.aspx
8http://msdn.microsoft.com/en-us/library/ww3k31w0.aspx
9http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx
10http://msdn.microsoft.com/en-us/library/system.data.datarowversion.aspx
11http://msdn.microsoft.com/en-us/library/w9y9a401.aspx
12http://msdn.microsoft.com/en-us/library/tzwewss0.aspx
13http://go.microsoft.com/fwlink/?LinkId=217917
Community Content Add
Annotations FAQ