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 属性为 falseValue 则使用带默认类型转换器的单元 ParseFormattedValue 方法分析 。 此方法的默认实现使用传入的单元格样式的 NullValueDataSourceNullValueFormatProvider 属性分析值。 如果值不等于 NullValue,则使用 FormatProvider 属性和传入的类型转换器分析该值。

若要自定义单元格值转换为格式化值以供显示的转换,请 CellFormatting 处理 事件。

有关如何处理事件的详细信息,请参阅 处理和引发事件

创建 DataGridViewCellParsingEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件

扩展方法

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

另请参阅