DataGridViewCellParsingEventHandler DataGridViewCellParsingEventHandler DataGridViewCellParsingEventHandler DataGridViewCellParsingEventHandler Delegate

定义

表示将处理 CellParsingDataGridView 事件的方法。Represents the method that will handle a CellParsing event of a 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)

参数

sender
Object Object Object Object

事件源。The source of the event.

继承
DataGridViewCellParsingEventHandlerDataGridViewCellParsingEventHandlerDataGridViewCellParsingEventHandlerDataGridViewCellParsingEventHandler

示例

下面的代码示例演示如何DataGridViewCellParsingEventHandler使用检查日期项的有效性。The following code example demonstrates using DataGridViewCellParsingEventHandler to check the validity of date entries.

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

注解

处理事件, 以提供自定义值从用户指定的值转换为 cell ValueType属性所指定的类型中的值。 CellParsingHandle the CellParsing event to provide custom value conversion from a user-specified value to a value in the type specified by the cell ValueType property.

处理CellParsing事件时, 可以自行转换值, 也可以自定义默认转换。When you handle the CellParsing event, you can convert the value yourself or you can customize the default conversion. 例如, 您可以使用单元格ParseFormattedValue方法和所选的类型转换器来自行转换值。For example, you can convert the value yourself using the cell ParseFormattedValue method with type converters of your choosing. 或者, 您可以让默认类型转换器分析值, 但是NullValue修改DataGridViewCellParsingEventArgs.InheritedCellStyle属性返回的对象DataSourceNullValue的、 FormatProvider和属性, 该属性是使用单元格InheritedStyle知识产权.Alternatively, you can let the default type converters parse the value, but modify the NullValue, DataSourceNullValue, and FormatProvider properties of the object returned by the DataGridViewCellParsingEventArgs.InheritedCellStyle property, which is initialized using the cell InheritedStyle property.

当你自行转换值时, 请将ConvertEventArgs.Value属性的初始格式化值替换为 cell ValueType属性所指定的类型中的转换后的值。When you convert the value yourself, replace the initial, formatted value of the ConvertEventArgs.Value property with the converted value in the type specified by the cell ValueType property. 若要指示无需进行进一步分析, 请将DataGridViewCellParsingEventArgs.ParsingApplied属性设置true为。To indicate that no further parsing is necessary, set the DataGridViewCellParsingEventArgs.ParsingApplied property to true.

当事件处理程序完成时, 如果Valuenull或不是ParsingApplied正确的类型, 或者属性为false, Value则使用带有默认类型转换器的单元ParseFormattedValue方法来分析。When the event handler completes, if the Value is null or is not of the correct type, or the ParsingApplied property is false, the Value is parsed using the cell ParseFormattedValue method with default type converters. 此方法的默认实现使用传入的单元格样式NullValueDataSourceNullValue、和FormatProvider属性分析值。The default implementation of this method parses the value using the NullValue, DataSourceNullValue, and FormatProvider properties of the cell style passed in. 如果该值不等于NullValue, 则FormatProvider使用属性和传入的类型转换器分析该值。If the value is not equal to NullValue, the value is parsed using the FormatProvider property and the type converters passed in.

若要自定义单元格值到格式化值的转换以便显示, 请处理CellFormatting事件。To customize the conversion of a cell value into a formatted value for display, handle the CellFormatting event.

有关如何处理事件的详细信息, 请参阅处理和引发事件For more information about how to handle events, see Handling and Raising Events.

创建 DataGridViewCellParsingEventHandler 委托时,需要标识将处理该事件的方法。When you create a DataGridViewCellParsingEventHandler delegate, you identify the method that will handle the event. 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。To associate the event with your event handler, add an instance of the delegate to the event. 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。The event handler is called whenever the event occurs, unless you remove the delegate. 有关事件处理程序委托的详细信息, 请参阅处理和引发事件For more information about event-handler delegates, see Handling and Raising Events.

扩展方法

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于

另请参阅