DataTable Edits

When you make changes to column values in a DataRow, the changes are immediately placed in the current state of the row. The DataRowState is then set to Modified, and the changes are accepted or rejected using the AcceptChanges or RejectChanges 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 BeginEdit, EndEdit, and CancelEdit.

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 Versions.

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.

Dim workTable As DataTable = New DataTable  
workTable.Columns.Add("LastName", Type.GetType("System.String"))  

AddHandler workTable.ColumnChanged, _  
  New DataColumnChangeEventHandler(AddressOf OnColumnChanged)  

Dim workRow As DataRow = workTable.NewRow()  
workRow(0) = "Smith"  

' Causes the ColumnChanged event to write a message and cancel the edit.  
workRow(0) = ""       

' Displays "Smith, New".  
Console.WriteLine("{0}, {1}", workRow(0), workRow.RowState)  

Private Shared Sub OnColumnChanged( _  
  sender As Object, args As DataColumnChangeEventArgs)  
  If args.Column.ColumnName = "LastName" Then  
    If args.ProposedValue.ToString() = "" Then  
      Console.WriteLine("Last Name cannot be blank.  Edit canceled.")  
    End If  
  End If  
End Sub  
DataTable workTable  = new DataTable();  
workTable.Columns.Add("LastName", typeof(String));  

workTable.ColumnChanged +=   
  new DataColumnChangeEventHandler(OnColumnChanged);  

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

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

// 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.");  

See Also

Manipulating Data in a DataTable
Handling DataTable Events
ADO.NET Managed Providers and DataSet Developer Center