DataGridViewCellParsingEventArgs DataGridViewCellParsingEventArgs DataGridViewCellParsingEventArgs DataGridViewCellParsingEventArgs Class

定义

CellParsing 控件的 DataGridView 事件提供数据。Provides data for the CellParsing event of a DataGridView control.

public ref class DataGridViewCellParsingEventArgs : System::Windows::Forms::ConvertEventArgs
public class DataGridViewCellParsingEventArgs : System.Windows.Forms.ConvertEventArgs
type DataGridViewCellParsingEventArgs = class
    inherit ConvertEventArgs
Public Class DataGridViewCellParsingEventArgs
Inherits ConvertEventArgs
继承
DataGridViewCellParsingEventArgsDataGridViewCellParsingEventArgsDataGridViewCellParsingEventArgsDataGridViewCellParsingEventArgs

示例

下面的代码示例演示了如何使用DataGridViewCellParsingEventArgs若要检查的日期项的有效性。The following code example demonstrates using DataGridViewCellParsingEventArgs 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

注解

处理CellParsing事件,以便提供从用户指定值到指定单元格的类型中的值的自定义值转换ValueType属性。Handle 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. 或者,可以让默认类型转换器分析的值,但需要修改NullValueDataSourceNullValue,并FormatProvider返回的对象的属性DataGridViewCellParsingEventArgs.InheritedCellStyle属性,则使用该单元格初始化InheritedStyle属性。Alternately, 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属性中指定单元格的类型转换后的值与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属性设置为trueTo 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.

构造函数

DataGridViewCellParsingEventArgs(Int32, Int32, Object, Type, DataGridViewCellStyle) DataGridViewCellParsingEventArgs(Int32, Int32, Object, Type, DataGridViewCellStyle) DataGridViewCellParsingEventArgs(Int32, Int32, Object, Type, DataGridViewCellStyle) DataGridViewCellParsingEventArgs(Int32, Int32, Object, Type, DataGridViewCellStyle)

初始化 DataGridViewCellParsingEventArgs 类的新实例。Initializes a new instance of the DataGridViewCellParsingEventArgs class.

属性

ColumnIndex ColumnIndex ColumnIndex ColumnIndex

获取需要分析的单元格数据的列索引。Gets the column index of the cell data that requires parsing.

DesiredType DesiredType DesiredType DesiredType

获取所需值的数据类型。Gets the data type of the desired value.

(Inherited from ConvertEventArgs)
InheritedCellStyle InheritedCellStyle InheritedCellStyle InheritedCellStyle

获取或设置应用于编辑的单元格的样式。Gets or sets the style applied to the edited cell.

ParsingApplied ParsingApplied ParsingApplied ParsingApplied

获取或设置一个值,该值指示是否已成功地对单元格的值进行分析。Gets or sets a value indicating whether a cell's value has been successfully parsed.

RowIndex RowIndex RowIndex RowIndex

获取需要分析的单元格的行索引。Gets the row index of the cell that requires parsing.

Value Value Value Value

获取或设置 ConvertEventArgs 的值。Gets or sets the value of the ConvertEventArgs.

(Inherited from ConvertEventArgs)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅