Working with DataTable Events 

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

Event Description

ColumnChanged

Occurs when a value has been inserted successfully into a column.

ColumnChanging

Occurs when a value has been submitted for a column.

RowChanged

Occurs after a row in the table has been edited successfully.

RowChanging

Occurs when a row in the table is changing.

RowDeleted

Occurs after a row in the table has been marked as Deleted.

RowDeleting

Occurs before a row in the table is marked as Deleted.

The following example creates four events: OnColumnChanged, OnColumnChanging, OnRowChanged, and OnRowChanging. Each of these events occurs when a column or row changes.

workTable.ColumnChanged  += new 
  DataColumnChangeEventHandler(OnColumnChanged);
workTable.ColumnChanging += new 
  DataColumnChangeEventHandler(OnColumnChanging);
workTable.RowChanged += new DataRowChangeEventHandler(OnRowChanged);
workTable.RowChanging += new DataRowChangeEventHandler(OnRowChanging);

protected static void OnColumnChanged(
  object sender, DataColumnChangeEventArgs args)
{
  Console.Write(" ColumnChanged: ");
  Console.Write(args.Column.ColumnName + " changed to '" + args.ProposedValue + "'\n");
}

protected static void OnColumnChanging(
  object sender, DataColumnChangeEventArgs args)
{
  Console.Write("ColumnChanging: ");
  Console.Write(args.Column.ColumnName + " equals '" + 
    args.Row[args.Column] + 
    "', changing to '" + args.ProposedValue + "'\n");    
}

protected static void OnRowChanging(
  object sender, DataRowChangeEventArgs args)
{
  if (args.Action != DataRowAction.Nothing)
    Console.WriteLine("   RowChanging: Action = " + args.Action + ",
      CustID = " + args.Row["CustID"]);
}

protected static void OnRowChanged(
  object sender, DataRowChangeEventArgs args)
{
  if (args.Action != DataRowAction.Nothing)
    Console.WriteLine("    RowChanged: Action = " + args.Action + ", 
      CustID = " + args.Row["CustID"]);
}

See Also

Show: