DataGridView.CellParsing DataGridView.CellParsing DataGridView.CellParsing DataGridView.CellParsing Event

定义

在单元格值已修改的情况下,当单元格退出编辑模式时发生。Occurs when a cell leaves edit mode if the cell value has been modified.

public:
 event System::Windows::Forms::DataGridViewCellParsingEventHandler ^ CellParsing;
public event System.Windows.Forms.DataGridViewCellParsingEventHandler CellParsing;
member this.CellParsing : System.Windows.Forms.DataGridViewCellParsingEventHandler 
Public Custom Event CellParsing As DataGridViewCellParsingEventHandler 

示例

下面的代码示例演示如何处理CellParsing事件。The following code example shows how to handle the CellParsing event. 它还演示如何使用DataGridViewCellParsingEventArgs类。It also shows how to use the DataGridViewCellParsingEventArgs class.

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

注解

默认情况下, DataGridView该控件将尝试将单元格中显示的用户指定值转换为 cell ValueType属性所指定的类型中的实际基础单元值。By default, the DataGridView control will attempt to convert a user-specified value displayed in a cell to an actual underlying cell value in the type specified by the cell ValueType property. 此转换使用 cell InheritedStyle属性返回的单元格样式的格式设置属性。This conversion uses the formatting properties of the cell style returned by the cell InheritedStyle property.

如果标准转换无法满足您的需要,请处理CellParsing事件以提供到所需类型的自定义值转换。If the standard conversion does not meet your needs, handle the CellParsing event to provide custom value conversion to the required type.

用户可以使用由EditMode属性指定的方法进入编辑模式,还可以退出编辑模式,通过移动到其他单元或按 enter 来提交对单元格所做的任何更改。Users can enter edit mode using the method specified by the EditMode property, and can leave edit mode, committing any changes to a cell, by moving to another cell or by pressing ENTER. 按 ESC 将在提交之前恢复对值所做的任何更改,并且CellParsing不会发生该事件。Pressing ESC will revert any changes to the value before it is committed, and the CellParsing event will not occur. 仅当单元格值已实际修改时才发生事件,即使最终值与原始值相同也是如此。CellParsingThe CellParsing event occurs only if the cell value has actually been modified, even if the final value is the same as the original value. 如果CommitEdit调用方法,也会发生这种情况。It also occurs when the CommitEdit method is called.

处理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.

当事件处理程序完成时,如果ConvertEventArgs.Valuenull或不是DataGridViewCellParsingEventArgs.ParsingApplied正确的类型,或者属性为falseValue则使用带有默认类型转换器的单元ParseFormattedValue方法来分析。When the event handler completes, if the ConvertEventArgs.Value is null or is not of the correct type, or the DataGridViewCellParsingEventArgs.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.

适用于

另请参阅