DataGridView.CellValidating Evento

Definición

Se produce cuando una celda pierde el foco de entrada. Habilita la validación de contenido.

C#
public event System.Windows.Forms.DataGridViewCellValidatingEventHandler CellValidating;
C#
public event System.Windows.Forms.DataGridViewCellValidatingEventHandler? CellValidating;

Tipo de evento

Ejemplos

En el ejemplo de código siguiente se controla el CellValidating evento para asegurarse de que el usuario escribe solo enteros positivos. Este ejemplo forma parte de un ejemplo más grande disponible en el tema de VirtualMode referencia.

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

Comentarios

Al cancelar este evento, se cancelan los cambios en la celda actual. Cuando este evento se cancela en modo enlazado a datos, el nuevo valor no se inserta en el origen de datos subyacente. Cuando este evento se cancela en modo virtual, no se generará el CellValuePushed evento.

Controle el evento para realizar el procesamiento posterior a la CellValidated validación.

Para obtener más información acerca de cómo controlar eventos, vea controlar y provocar eventos.

Se aplica a

Producto Versiones
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Consulte también