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