DataGridViewCellParsingEventHandler Delegar

Definição

Representa o método que manipulará um evento CellParsing de um 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

A fonte do evento.

Exemplos

O exemplo de código a seguir demonstra o uso DataGridViewCellParsingEventHandler para marcar a validade das entradas de data.

// 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

Comentários

Manipule o CellParsing evento para fornecer conversão de valor personalizado de um valor especificado pelo usuário para um valor no tipo especificado pela propriedade de célula ValueType .

Ao manipular o CellParsing evento, você pode converter o valor por conta própria ou personalizar a conversão padrão. Por exemplo, você pode converter o valor por conta própria usando o método de célula ParseFormattedValue com conversores de tipo de sua escolha. Como alternativa, você pode permitir que os conversores de tipo padrão analisem o valor, mas modifiquem as NullValuepropriedades , DataSourceNullValuee FormatProvider do objeto retornado pela DataGridViewCellParsingEventArgs.InheritedCellStyle propriedade , que é inicializada usando a propriedade cell InheritedStyle .

Ao converter o valor por conta própria, substitua o valor inicial formatado da ConvertEventArgs.Value propriedade pelo valor convertido no tipo especificado pela propriedade de célula ValueType . Para indicar que nenhuma análise adicional é necessária, defina a DataGridViewCellParsingEventArgs.ParsingApplied propriedade como true.

Quando o manipulador de eventos é concluído, se o Value é null ou não do tipo correto, ou a ParsingApplied propriedade é false, o Value é analisado usando o método de célula ParseFormattedValue com conversores de tipo padrão. A implementação padrão desse método analisa o valor usando as NullValuepropriedades , DataSourceNullValuee FormatProvider do estilo de célula passado. Se o valor não for igual a NullValue, o valor será analisado usando a FormatProvider propriedade e os conversores de tipo passados.

Para personalizar a conversão de um valor de célula em um valor formatado para exibição, manipule o CellFormatting evento.

Para obter mais informações sobre como lidar com eventos, consulte Manipulando e levantando eventos.

Ao criar um DataGridViewCellParsingEventHandler delegado, você identifica o método que manipulará o evento. Para associar o evento ao manipulador de eventos, adicione uma instância do delegado ao evento. O manipulador de eventos é chamado sempre que o evento ocorre, a menos que você remova o representante. Para obter mais informações sobre delegados do manipulador de eventos, consulte Manipulando e levantando eventos.

Métodos de Extensão

GetMethodInfo(Delegate)

Obtém um objeto que representa o método representado pelo delegado especificado.

Aplica-se a

Confira também