Sdílet prostřednictvím


DataGridViewCellParsingEventHandler Delegát

Definice

Představuje metodu, která bude zpracovávat CellParsing událost 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

Zdroj události

Příklady

Následující příklad kódu ukazuje použití DataGridViewCellParsingEventHandler ke kontrole platnosti položek kalendářních dat.

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

Poznámky

CellParsing Zpracujte událost tak, aby poskytovala převod vlastní hodnoty z uživatelem zadané hodnoty na hodnotu v typu určeném vlastností buňkyValueType.

Při zpracování CellParsing události můžete hodnotu převést sami nebo můžete přizpůsobit výchozí převod. Hodnotu můžete například převést sami pomocí metody buňky ParseFormattedValue s převaděči typů podle vašeho výběru. Alternativně můžete nechat výchozí převaděče typů parsovat hodnotu, ale upravit , a vlastnosti objektu vráceného DataGridViewCellParsingEventArgs.InheritedCellStyle vlastností, který je inicializován pomocí vlastnosti buňkyInheritedStyle.FormatProviderDataSourceNullValueNullValue

Při převodu hodnoty sami nahraďte počáteční formátovanou hodnotu ConvertEventArgs.Value vlastnosti převedenou hodnotou v typu určeném vlastností buňky ValueType . Chcete-li označit, že žádná další analýza není nutná, nastavte DataGridViewCellParsingEventArgs.ParsingApplied vlastnost na truehodnotu .

Po dokončení obslužné rutiny události, pokud Value je null nebo není správný typ, nebo ParsingApplied je falsevlastnost je , Value je analyzována pomocí metody buňky ParseFormattedValue s výchozími převaděči typů. Výchozí implementace této metody parsuje hodnotu pomocí NullValue, DataSourceNullValuea FormatProvider vlastnosti stylu buňky předané. Pokud hodnota není rovna NullValue, hodnota je analyzována pomocí FormatProvider vlastnosti a převaděče typů předané.

Pokud chcete přizpůsobit převod hodnoty buňky na formátovanou hodnotu pro zobrazení, zpracujte CellFormatting událost.

Další informace o zpracování událostí naleznete v tématu Zpracování a vyvolávání událostí.

Při vytváření delegáta DataGridViewCellParsingEventHandler identifikujete metodu, která bude zpracovávat událost. Pokud chcete událost přidružit k obslužné rutině události, přidejte do události instanci delegáta. Obslužná rutina události se volá při každém výskytu události, pokud delegáta neodeberete. Další informace o delegátech obslužné rutiny událostí naleznete v tématu Zpracování a vyvolávání událostí.

Metody rozšíření

Name Description
GetMethodInfo(Delegate)

Získá objekt, který představuje metodu reprezentovanou zadaným delegátem.

Platí pro

Viz také