DataGridViewCellParsingEventHandler Delegado

Definición

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

public delegate void DataGridViewCellParsingEventHandler(System::Object ^ sender, DataGridViewCellParsingEventArgs ^ e);
public delegate void DataGridViewCellParsingEventHandler(object sender, DataGridViewCellParsingEventArgs e);
public delegate void DataGridViewCellParsingEventHandler(object? sender, DataGridViewCellParsingEventArgs e);
type DataGridViewCellParsingEventHandler = delegate of obj * DataGridViewCellParsingEventArgs -> unit
Public Delegate Sub DataGridViewCellParsingEventHandler(sender As Object, e As DataGridViewCellParsingEventArgs)

Parámetros

sender
Object

Origen del evento.

e
DataGridViewCellParsingEventArgs

Objeto DataGridViewCellParsingEventArgs que contiene los datos del evento.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar DataGridViewCellParsingEventHandler para comprobar la validez de las entradas de fecha.

// Handling CellParsing allows one to accept user input, then map it to a different
// internal representation.
void dataGridView1_CellParsing( Object^ /*sender*/, DataGridViewCellParsingEventArgs^ e )
{
   if ( this->dataGridView1->Columns[ e->ColumnIndex ]->Name->Equals( "Release Date" ) )
   {
      if ( e != nullptr )
      {
         if ( e->Value != nullptr )
         {
            try
            {
               // Map what the user typed into UTC.
               e->Value = DateTime::Parse( e->Value->ToString() ).ToUniversalTime();

               // Set the ParsingApplied property to 
               // Show the event is handled.
               e->ParsingApplied = true;
            }
            catch ( FormatException^ /*ex*/ ) 
            {
               // Set to false in case another CellParsing handler
               // wants to try to parse this DataGridViewCellParsingEventArgs instance.
               e->ParsingApplied = false;
            }
         }
      }
   }
}
// Handling CellParsing allows one to accept user input, then map it to a different
// internal representation.
private void dataGridView1_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date")
    {
        if (e != null)
        {
            if (e.Value != null)
            {
                try
                {
                    // Map what the user typed into UTC.
                    e.Value = DateTime.Parse(e.Value.ToString()).ToUniversalTime();
                    // Set the ParsingApplied property to 
                    // Show the event is handled.
                    e.ParsingApplied = true;
                }
                catch (FormatException)
                {
                    // Set to false in case another CellParsing handler
                    // wants to try to parse this DataGridViewCellParsingEventArgs instance.
                    e.ParsingApplied = false;
                }
            }
        }
    }
}
' Handling CellParsing allows one to accept user input, then map it to a different
' internal representation.
Private Sub dataGridView1_CellParsing(ByVal sender As Object, _
    ByVal e As DataGridViewCellParsingEventArgs) _
    Handles dataGridView1.CellParsing

    If Me.dataGridView1.Columns(e.ColumnIndex).Name = _
        "Release Date" Then
        If e IsNot Nothing Then
            If e.Value IsNot Nothing Then
                Try
                    ' Map what the user typed into UTC.
                    e.Value = _
                    DateTime.Parse(e.Value.ToString()).ToUniversalTime()
                    ' Set the ParsingApplied property to 
                    ' Show the event is handled.
                    e.ParsingApplied = True

                Catch ex As FormatException
                    ' Set to false in case another CellParsing handler
                    ' wants to try to parse this DataGridViewCellParsingEventArgs instance.
                    e.ParsingApplied = False
                End Try
            End If
        End If
    End If
End Sub

Comentarios

Controle el CellParsing evento para proporcionar una conversión de valor personalizada de un valor especificado por el usuario a un valor en el tipo especificado por la propiedad cell ValueType .

Al controlar el CellParsing evento, puede convertir el valor usted mismo o personalizar la conversión predeterminada. Por ejemplo, puede convertir el valor usted mismo mediante el método de celda ParseFormattedValue con convertidores de tipos de su elección. Como alternativa, puede permitir que los convertidores de tipos predeterminados analicen el valor, pero modifiquen las NullValuepropiedades , DataSourceNullValuey FormatProvider del objeto devuelto por la DataGridViewCellParsingEventArgs.InheritedCellStyle propiedad , que se inicializa mediante la propiedad cell InheritedStyle .

Al convertir el valor usted mismo, reemplace el valor inicial con formato de la ConvertEventArgs.Value propiedad por el valor convertido en el tipo especificado por la propiedad cell ValueType . Para indicar que no es necesario analizar más, establezca la DataGridViewCellParsingEventArgs.ParsingApplied propiedad trueen .

Cuando se completa el controlador de eventos, si Value es null o no es del tipo correcto, o la ParsingApplied propiedad es false, Value se analiza mediante el método de celda ParseFormattedValue con convertidores de tipos predeterminados. La implementación predeterminada de este método analiza el valor mediante las NullValuepropiedades , DataSourceNullValuey FormatProvider del estilo de celda pasado. Si el valor no es igual a NullValue, el valor se analiza mediante la FormatProvider propiedad y los convertidores de tipos pasados.

Para personalizar la conversión de un valor de celda en un valor con formato para mostrar, controle el CellFormatting evento.

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

Cuando se crea un delegado DataGridViewCellParsingEventHandler, se identifica el método que controlará el evento. Para asociar el evento al controlador, se debe agregar una instancia del delegado al evento. Siempre que se produce el evento, se llama a su controlador, a menos que se quite el delegado. Para obtener más información sobre los delegados del controlador de eventos, vea Control y generación de eventos.

Métodos de extensión

GetMethodInfo(Delegate)

Obtiene un objeto que representa el método representado por el delegado especificado.

Se aplica a

Consulte también