DataGridViewCellParsingEventHandler Delegat
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Reprezentuje metodę, która będzie obsługiwać CellParsing zdarzenie klasy 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)
Parametry
- sender
- Object
Źródło zdarzenia.
Element DataGridViewCellParsingEventArgs zawierający dane zdarzenia.
Przykłady
W poniższym przykładzie kodu pokazano użycie metody DataGridViewCellParsingEventHandler w celu sprawdzenia ważności wpisów daty.
// 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
Uwagi
Obsłuż zdarzenie, CellParsing aby zapewnić konwersję wartości niestandardowej z wartości określonej przez użytkownika na wartość w typie określonym przez właściwość komórki ValueType .
Podczas obsługi CellParsing zdarzenia można przekonwertować wartość samodzielnie lub dostosować konwersję domyślną. Możesz na przykład przekonwertować wartość samodzielnie przy użyciu metody komórki ParseFormattedValue z wybranymi konwerterami typów. Alternatywnie można zezwolić domyślnym konwerterom typów na analizowanie wartości, ale zmodyfikować NullValuewłaściwości , DataSourceNullValuei FormatProvider obiektu zwróconego przez DataGridViewCellParsingEventArgs.InheritedCellStyle właściwość , który jest inicjowany przy użyciu właściwości komórki InheritedStyle .
Podczas samodzielnej konwersji wartości zastąp początkową, sformatowaną wartość ConvertEventArgs.Value właściwości wartością przekonwertowaną w typie określonym przez właściwość komórki ValueType . Aby wskazać, że nie jest konieczne dalsze analizowanie, ustaw DataGridViewCellParsingEventArgs.ParsingApplied właściwość na true.
Po zakończeniu procedury obsługi zdarzeń, jeśli element Value jest null lub nie ma poprawnego typu, lub ParsingApplied właściwość ma falsewartość , Value parametr jest analizowany przy użyciu metody komórki ParseFormattedValue z domyślnymi konwerterami typów. Domyślna implementacja tej metody analizuje wartość przy użyciu NullValuewłaściwości , DataSourceNullValuei FormatProvider stylu komórki przekazanego. Jeśli wartość nie jest równa NullValuewartości , wartość jest analizowana przy użyciu FormatProvider właściwości i konwerterów typów przekazanych.
Aby dostosować konwersję wartości komórki do sformatowanej wartości do wyświetlania, obsłuż CellFormatting zdarzenie.
Aby uzyskać więcej informacji na temat obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.
Podczas tworzenia delegata DataGridViewCellParsingEventHandler należy zidentyfikować metodę, która będzie obsługiwać zdarzenie. Aby skojarzyć zdarzenie z programem obsługi zdarzeń, dodaj wystąpienie delegata do zdarzenia. Program obsługi zdarzeń jest wywoływany przy każdym wystąpieniu zdarzenia, o ile nie usunięto delegata. Aby uzyskać więcej informacji na temat delegatów programu obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.
Metody rozszerzania
| GetMethodInfo(Delegate) |
Pobiera obiekt reprezentujący metodę reprezentowaną przez określonego delegata. |