Handling DataTable Events


The DataTable object provides a series of events that can be processed by an application. The following table describes DataTable events.

InitializedOccurs after the EndInit method of a DataTable is called. This event is intended primarily to support design-time scenarios.
ColumnChangedOccurs after a value has been successfully changed in a DataColumn.
ColumnChangingOccurs when a value has been submitted for a DataColumn.
RowChangedOccurs after a DataColumn value or the RowState of a DataRow in the DataTable has been changed successfully.
RowChangingOccurs when a change has been submitted for a DataColumn value or the RowState of a DataRow in the DataTable.
RowDeletedOccurs after a DataRow in the DataTable has been marked as Deleted.
RowDeletingOccurs before a DataRow in the DataTable is marked as Deleted.
TableClearedOccurs after a call to the Clear method of the DataTable has successfully cleared every DataRow.
TableClearingOccurs after the Clear method is called but before the Clear operation begins.
TableNewRowOccurs after a new DataRow is created by a call to the NewRow method of the DataTable.
DisposedOccurs when the DataTable is Disposed. Inherited from MarshalByValueComponent.
System_CAPS_ICON_note.jpg Note

Most operations that add or delete rows do not raise the ColumnChanged and ColumnChanging events. However, the ReadXml method does raise ColumnChanged and ColumnChanging events, unless the XmlReadMode is set to DiffGram or is set to Auto when the XML document being read is a DiffGram.

System_CAPS_ICON_warning.jpg Warning

Data corruption can occur if data is modified in a DataSet from which the RowChanged event is raised. No exception will be raised if such data corruption occurs.

The Constraints property holds a ConstraintCollection instance. The ConstraintCollection class exposes a CollectionChanged event. This event fires when a constraint is added, modified, or removed from the ConstraintCollection.

The Columns property holds a DataColumnCollection instance. The DataColumnCollection class exposes a CollectionChanged event. This event fires when a DataColumn is added, modified, or removed from the DataColumnCollection. Modifications that cause the event to fire include changes to the name, type, expression or ordinal position of a column.

The Tables property of a DataSet holds a DataTableCollection instance. The DataTableCollection class exposes both a CollectionChanged and a CollectionChanging event. These events fire when a DataTable is added to or removed from the DataSet.

Changes to DataRows can also trigger events for an associated DataView. The DataView class exposes a ListChanged event that fires when a DataColumn value changes or when the composition or sort order of the view changes. The DataRowView class exposes a PropertyChanged event that fires when an associated DataColumn value changes.

Here is the sequence of operations that occur when a DataRow is added, modified, or deleted:

  1. Create the proposed record and apply any changes.

  2. Check constraints for non-expression columns.

  3. Raise the RowChanging or RowDeleting events as applicable.

  4. Set the proposed record to be the current record.

  5. Update any associated indexes.

  6. Raise ListChanged events for associated DataView objects and PropertyChanged events for associated DataRowView objects.

  7. Evaluate all expression columns, but delay checking any constraints on these columns.

  8. Raise ListChanged events for associated DataView objects and PropertyChanged events for associated DataRowView objects affected by the expression column evaluations.

  9. Raise RowChanged or RowDeleted events as applicable.

  10. Check constraints on expression columns.

System_CAPS_ICON_note.jpg Note

Changes to expression columns never raise DataTable events. Changes to expression columns only raise DataView and DataRowView events. Expression columns can have dependencies on multiple other columns, and can be evaluated multiple times during a single DataRow operation. Each expression evaluation raises events, and a single DataRow operation can raise multiple ListChanged and PropertyChanged events when expression columns are affected, possibly including multiple events for the same expression column.

System_CAPS_ICON_warning.jpg Warning

Do not throw a NullReferenceException within the RowChanged event handler. If a NullReferenceException is thrown within the RowChanged event of a DataTable, then the DataTable will be corrupted.


The following example demonstrates how to create event handlers for the RowChanged, RowChanging, RowDeleted, RowDeleting, ColumnChanged, ColumnChanging, TableNewRow, TableCleared, and TableClearing events. Each event handler displays output in the console window when it is fired.

[!CODE [DataWorks DataTable.Events#1](../CodeSnippet/VS_Snippets_ADO.NET/DataWorks DataTable.Events#1)]

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