DataGridViewCellParsingEventHandler Delegar
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Representa o método que irá tratar um CellParsing evento de um DataGridView.
public delegate void DataGridViewCellParsingEventHandler(System::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 origem do evento.
A DataGridViewCellParsingEventArgs que contém os dados do evento.
Exemplos
O exemplo de código seguinte demonstra a utilização DataGridViewCellParsingEventHandler para verificar 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
Observações
Gerir o CellParsing evento para fornecer uma conversão personalizada de valor de um valor especificado pelo utilizador para um valor do tipo especificado pela propriedade da célula ValueType .
Quando gerir o CellParsing evento, pode converter o valor você próprio ou pode personalizar a conversão padrão. Por exemplo, pode converter o valor por si mesmo usando o método da célula ParseFormattedValue com conversores de tipos à sua escolha. Alternativamente, pode deixar os conversores de tipos padrão analisarem o valor, mas modificar o NullValue, , e FormatProvider as propriedades do objeto devolvido pela DataGridViewCellParsingEventArgs.InheritedCellStyle propriedade, que é inicializada usando a propriedade da célula InheritedStyleDataSourceNullValue.
Quando converter o valor por si próprio, substitua o valor inicial formatado da ConvertEventArgs.Value propriedade pelo valor convertido no tipo especificado pela propriedade da célula ValueType . Para indicar que não é necessária mais análise sintática, defina a DataGridViewCellParsingEventArgs.ParsingApplied propriedade para true.
Quando o gestor de eventos termina, se o Value for ou não do tipo correto, ou a ParsingApplied propriedade for false, o Value é analisado usando o método da célula ParseFormattedValue com conversores de tipos por defeitonull. A implementação padrão deste método analisa o valor usando , NullValueDataSourceNullValue, e FormatProvider propriedades do estilo de célula passado. Se o valor não for igual a NullValue, o valor é analisado usando a FormatProvider propriedade e os conversores de tipos são passados.
Para personalizar a conversão de um valor de célula num valor formatado para exibição, trate do CellFormatting evento.
Para mais informações sobre como gerir eventos, consulte Gestão e Levantamento de Eventos.
Quando cria um DataGridViewCellParsingEventHandler delegado, identifica o método que irá gerir o evento. Para associar o evento ao seu gestor de eventos, adicione uma instância do delegado ao evento. O gestor de eventos é chamado sempre que o evento ocorre, a menos que remova o delegado. Para mais informações sobre os delegados handlers de eventos, consulte Gestão e Angariação de Eventos.
Métodos da Extensão
| Name | Description |
|---|---|
| GetMethodInfo(Delegate) |
Obtém um objeto que representa o método representado pelo delegado especificado. |