Share via


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 prior to a row in the table being 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.

AddHandler workTable.ColumnChanged,  New DataColumnChangeEventHandler(AddressOf OnColumnChanged)
AddHandler workTable.ColumnChanging, New DataColumnChangeEventHandler(AddressOf OnColumnChanging)
AddHandler workTable.RowChanged,     New DataRowChangeEventHandler(AddressOf OnRowChanged)
AddHandler workTable.RowChanging,    New DataRowChangeEventHandler(AddressOf OnRowChanging)

Private Shared Sub OnColumnChanged(sender As Object, args As DataColumnChangeEventargs)
  Console.Write(" ColumnChanged: ")
  Console.Write(args.Column.ColumnName & " changed to '" & args.ProposedValue.ToString() & "'" & vbCrLf)
End Sub

Private Shared Sub OnColumnChanging(sender As Object, args As DataColumnChangeEventargs)
  Console.Write("ColumnChanging: ")
  Console.Write(args.Column.ColumnName & " equals '" & args.Row(args.Column).ToString() & _
                    "', changing to '" & args.ProposedValue.ToString() & "'" & vbCrLf)    
End Sub

Private Shared Sub OnRowChanging(sender As Object, args As DataRowChangeEventargs)
  If args.Action <> DataRowAction.Nothing Then
    Dim actionStr As String
    actionStr = System.Enum.GetName(args.Action.GetType(), args.Action)

    Console.WriteLine("   RowChanging: Action = " & actionStr & ", CustID = " & args.Row("CustID").ToString())
  End If
End Sub

Private Shared Sub OnRowChanged(sender As Object, args As DataRowChangeEventargs)
  If args.Action <> DataRowAction.Nothing Then
    Dim actionStr As String
    actionStr = System.Enum.GetName(args.Action.GetType(), args.Action)

    Console.WriteLine("    RowChanged: Action = " & actionStr & ", CustID = " & args.Row("CustID").ToString())
  End If
End Sub
[C#]
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

Manipulating Data in a DataTable | DataTable Class