閱讀英文

共用方式為


DataGridViewCellParsingEventHandler 代理人

定義

表示將處理 CellParsingDataGridView 事件的方法。

C#
public delegate void DataGridViewCellParsingEventHandler(object sender, DataGridViewCellParsingEventArgs e);
C#
public delegate void DataGridViewCellParsingEventHandler(object? sender, DataGridViewCellParsingEventArgs e);

參數

sender
Object

事件的來源。

範例

下列程式碼範例示範如何使用 DataGridViewCellParsingEventHandler 來檢查日期專案的有效性。

C#
// 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;
                }
            }
        }
    }
}

備註

CellParsing處理 事件,以提供自訂值從使用者指定的值轉換為儲存格 ValueType 屬性所指定型別中的值。

當您處理 CellParsing 事件時,您可以自行轉換值,也可以自訂預設轉換。 例如,您可以使用具有您選擇的類型轉換器的儲存格 ParseFormattedValue 方法,自行轉換值。 或者,您可以讓預設型別轉換器剖析值,但修改 NullValue 屬性所 DataGridViewCellParsingEventArgs.InheritedCellStyle 傳回物件的 、 DataSourceNullValueFormatProvider 屬性,該屬性是使用儲存格 InheritedStyle 屬性初始化。

當您自行轉換值時,請將 屬性的初始格式值 ConvertEventArgs.Value 取代為儲存格 ValueType 屬性所指定型別中的已轉換值。 若要指出不需要進一步剖析,請將 DataGridViewCellParsingEventArgs.ParsingApplied 屬性設定為 true

當事件處理常式完成時,如果 Valuenull 或 不是正確的型別,或 ParsingApplied 屬性為 false ,則會 Value 使用具有預設類型轉換子的儲存格 ParseFormattedValue 方法剖析 。 此方法的預設實作會使用 NullValue 傳入之儲存格樣式的 、 DataSourceNullValueFormatProvider 屬性來剖析值。 如果值不等於 NullValue ,則會使用 FormatProvider 屬性和傳入的類型轉換子剖析值。

若要自訂儲存格值轉換成格式化的值以供顯示,請處理 CellFormatting 事件。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

建立 DataGridViewCellParsingEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 處理和引發事件

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

產品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另請參閱