DataTable イベントの使用
DataTable オブジェクトは、アプリケーションが処理できる一連のイベントを提供します。DataTable イベントの説明を次の表に示します。
イベント | 説明 |
---|---|
ColumnChanged | 値が列に正常に挿入されたときに発生します。 |
ColumnChanging | 列に対して値が提示されたときに発生します。 |
RowChanged | テーブル内の行が正常に編集された後に発生します。 |
RowChanging | テーブル内の行が変更されるときに発生します。 |
RowDeleted | テーブル内の行が Deleted とマークされた後に発生します。 |
RowDeleting | テーブル内の行が Deleted としてマークされる前に発生します。 |
4 つのイベント OnColumnChanged、OnColumnChanging、OnRowChanged、および OnRowChanging を作成する例を次に示します。これらの各イベントは、列または行が変更されるときに発生します。
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"]);
}