Evaluar y enviar comentarios
MSDN
MSDN Library
 DataGridViewCellValidatingEventHand...
Contraer todo/Expandir todo Contraer todo
Esta página es específica de
Microsoft Visual Studio 2008/.NET Framework 3.5

Hay además otras versiones disponibles para:
Biblioteca de clases de .NET Framework
DataGridViewCellValidatingEventHandler (Delegado)

Actualización: noviembre 2007

Representa el método que controlará el evento CellValidating de un control DataGridView.

Espacio de nombres:  System.Windows.Forms
Ensamblado:  System.Windows.Forms (en System.Windows.Forms.dll)
Visual Basic (Declaración)
Public Delegate Sub DataGridViewCellValidatingEventHandler ( _
    sender As Object, _
    e As DataGridViewCellValidatingEventArgs _
)
Visual Basic (Uso)
Dim instance As New DataGridViewCellValidatingEventHandler(AddressOf HandlerMethod)
C#
public delegate void DataGridViewCellValidatingEventHandler(
    Object sender,
    DataGridViewCellValidatingEventArgs e
)
Visual C++
public delegate void DataGridViewCellValidatingEventHandler(
    Object^ sender, 
    DataGridViewCellValidatingEventArgs^ e
)
J#
/** @delegate */
public delegate void DataGridViewCellValidatingEventHandler(
    Object sender,
    DataGridViewCellValidatingEventArgs e
)
JScript
JScript no admite delegados.

Parámetros

sender
Tipo: System..::.Object
Referencia al remitente del evento.
e
Tipo: System.Windows.Forms..::.DataGridViewCellValidatingEventArgs
Objeto DataGridViewCellValidatingEventArgs que contiene los datos del evento.

Se provoca el evento CellValidating cuando una celda pierde el foco de entrada, lo que permite la validación del contenido. Al cancelar este evento, se cancelan los cambios realizados en la celda actual. Si el evento se cancela en modo enlazado a datos, el nuevo valor no se inserta en el origen de datos subyacente. Si el evento se cancela en modo virtual, no se provoca el evento CellValuePushed.

Cuando se crea un delegado DataGridViewCellValidatingEventHandler, se identifica el método que controlará el evento. Para asociar el evento al controlador de eventos, se debe agregar una instancia del delegado al evento. Siempre que se produzca el evento, se llamará al controlador de eventos, a menos que se quite el delegado. Para obtener más información sobre los delegados de controladores de eventos, vea Eventos y delegados.

En el siguiente ejemplo de código se muestra la forma de controlar el evento CellValidating para garantizar que el usuario sólo especifique números enteros positivos. Este ejemplo forma parte de un ejemplo más extenso que se encuentra disponible en el tema de referencia de VirtualMode.

Visual Basic
Private Sub dataGridView1_CellValidating(ByVal sender As Object, _
    ByVal e _
    As DataGridViewCellValidatingEventArgs) _
    Handles dataGridView1.CellValidating

    Me.dataGridView1.Rows(e.RowIndex).ErrorText = ""
    Dim newInteger As Integer

    ' Don't try to validate the 'new row' until finished 
    ' editing since there
    ' is not any point in validating its initial value.
    If dataGridView1.Rows(e.RowIndex).IsNewRow Then Return
    If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) _
        OrElse newInteger < 0 Then

        e.Cancel = True
        Me.dataGridView1.Rows(e.RowIndex).ErrorText = "the value must be a non-negative integer"

    End If
End Sub
C#
private void dataGridView1_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
{
    dataGridView1.Rows[e.RowIndex].ErrorText = "";
    int newInteger;

    // Don't try to validate the 'new row' until finished 
    // editing since there
    // is not any point in validating its initial value.
    if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
    if (!int.TryParse(e.FormattedValue.ToString(),
        out newInteger) || newInteger < 0)
    {
        e.Cancel = true;
        dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer";
    }
}
Visual C++
void VirtualConnector::dataGridView1_CellValidating
    (Object^ sender, DataGridViewCellValidatingEventArgs^ e)
{
    int newInteger;

    // Don't try to validate the 'new row' until finished 
    // editing since there
    // is not any point in validating its initial value.
    if (dataGridView1->Rows[e->RowIndex]->IsNewRow) 
    {
        return; 
    }
    if (!Int32::TryParse(e->FormattedValue->ToString(), 
        newInteger) || (newInteger < 0))
    {
        e->Cancel = true;
    }
}
J#
private void dataGridView1_CellValidating(Object sender,
    DataGridViewCellValidatingEventArgs e)
{
    int newInteger = 0;
    // Don't try to validate the 'new row' until finished 
    // editing since there
    // is not any point in validating its initial value.
    if (dataGridView1.get_Rows().get_Item(
        e.get_RowIndex()).get_IsNewRow()) {
        return;
    }
    if (!(Int32.TryParse(e.get_FormattedValue().ToString(), newInteger)) 
        || newInteger < 0) {
        e.set_Cancel(true);
    }
} //dataGridView1_CellValidating

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

.NET Framework

Compatible con: 3.5, 3.0, 2.0
Contenido de la comunidad   ¿Qué es Community Content?
Agregar contenido nuevo RSS  Anotaciones
Processing
© 2012 Microsoft. Reservados todos los derechos. Términos de uso | Marcas Registradas | Privacidad
Page view tracker