Compartir a través de


Trabajar con eventos DataTable

El objeto DataTable proporciona una serie de eventos que una aplicación puede procesar. En la siguiente tabla se describen los eventos de DataTable.

Evento Descripción
ColumnChanged Se produce cuando se inserta correctamente un valor en una columna.
ColumnChanged Se produce cuando se envía un valor para una columna.
RowChanged Se produce después de editar correctamente una fila de la tabla.
RowChanging Se produce mientras está cambiando una fila de la tabla.
RowDeleted Se produce después de que se haya marcado como Deleted una fila de la tabla.
RowDeleting Se produce antes de que se haya marcado como Deleted una fila de la tabla.

En el ejemplo siguiente se crean cuatro eventos: OnColumnChanged, OnColumnChanging, OnRowChanged y OnRowChanging. Cada uno de los eventos se produce cuando cambia una columna o una fila.

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

Vea también

Manipular datos en DataTable | DataTable (Clase)