DataTable Events

The following section describes DataTable events.

ColumnChanged and ColumnChanging

The ColumnChanged and ColumnChanging events can be handled to validate data or control user interface elements. The ColumnChanging event is raised when a value is being changed in a specified column; the ColumnChanged event is raised after the value in the column has been changed. Both events pass a DataColumnChangeEventArgs argument to the event handler that provide information specific to the event.

The following code demonstrates handling the ColumnChanging and ColumnChanged events to perform data validation and logging:

DataTable dt = new DataTable();

dt.ColumnChanged += new DataColumnChangeEventHandler(dt_ColumnChanged);

dt.ColumnChanging += new DataColumnChangeEventHandler(dt_ColumnChanging);

private static void dt_ColumnChanging(object sender,

    DataColumnChangeEventArgs e);


    if (e.Column.ColumnName == "MyColumn")


        if(e.ProposedValue.Equals("Invalid Data")


            e.Row.RowError = "Invalid data.";

            e.Row.SetColumnError(e.Column, "Column value " +

                "cannot be " e.ProposedValue.ToString());    




private static void ds_ColumnChanged(object sender,

    DataColumnChangeEventArgs e);


    System.IO.TextWriter tw = System.IO.File.AppendText("colchange.log");

    tw.WriteLine("ColumnChanging: Name = {0}; ProposedValue = {1}; " +

        "Row Id = {2}", e.ColumnName, e.ProposedValue.ToString(),




RowChanged, RowChanging, RowDeleted, and RowDeleting

The DataTable raises four ...

Get ADO.NET in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.