DataGridViewCellParsingEventArgs 类      
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 CellParsing 控件的 DataGridView 事件提供数据。
public ref class DataGridViewCellParsingEventArgs : System::Windows::Forms::ConvertEventArgspublic class DataGridViewCellParsingEventArgs : System.Windows.Forms.ConvertEventArgstype DataGridViewCellParsingEventArgs = class
    inherit ConvertEventArgsPublic Class DataGridViewCellParsingEventArgs
Inherits ConvertEventArgs- 继承
示例
下面的代码示例演示如何使用 DataGridViewCellParsingEventArgs 来检查日期条目的有效性。
// 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属性所指定类型中的值的自定义值转换。
处理 CellParsing 事件时,可以自行转换值,也可以自定义默认转换。 例如,可以使用具有所选类型转换器的单元格 ParseFormattedValue 方法自行转换值。 或者,可以让默认类型转换器分析值,但修改 NullValue属性DataGridViewCellParsingEventArgs.InheritedCellStyle返回的 对象的 、 DataSourceNullValue和 FormatProvider 属性,该属性使用单元InheritedStyle属性初始化。
在自行转换值时,请将属性的初始格式化值 ConvertEventArgs.Value 替换为单元格 ValueType 属性指定的类型中的转换值。 若要指示无需进一步分析,请将 DataGridViewCellParsingEventArgs.ParsingApplied 属性设置为 true。
事件处理程序完成后,如果 Value 为 null 或 不是正确类型,或者 ParsingApplied 属性为 false, Value 则使用带默认类型转换器的单元 ParseFormattedValue 方法分析 。 此方法的默认实现使用传入的单元格样式的 NullValue、 DataSourceNullValue和 FormatProvider 属性分析值。 如果值不等于 NullValue,则使用 FormatProvider 属性和传入的类型转换器分析该值。
若要自定义单元格值转换为格式化值以供显示的转换,请 CellFormatting 处理 事件。
有关如何处理事件的详细信息,请参阅 处理和引发事件。
构造函数
| DataGridViewCellParsingEventArgs(Int32, Int32, Object, Type, DataGridViewCellStyle) | 初始化 DataGridViewCellParsingEventArgs 类的新实例。 | 
属性
| ColumnIndex | 获取需要分析的单元格数据的列索引。 | 
| DesiredType | 获取所需值的数据类型。(继承自 ConvertEventArgs) | 
| InheritedCellStyle | 获取或设置应用于编辑的单元格的样式。 | 
| ParsingApplied | 获取或设置一个值,该值指示是否已成功地对单元格的值进行分析。 | 
| RowIndex | 获取需要分析的单元格的行索引。 | 
| Value | 获取或设置 ConvertEventArgs 的值。(继承自 ConvertEventArgs) | 
方法
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
适用于
另请参阅
- DataGridView
- CellParsing
- OnCellParsing(DataGridViewCellParsingEventArgs)
- CellFormatting
- EditMode
- ValueType
- InheritedStyle
- ParseFormattedValue(Object, DataGridViewCellStyle, TypeConverter, TypeConverter)
- DataGridViewCellParsingEventHandler
- Value
- InheritedCellStyle
- ParsingApplied
- DataGridViewCellStyle
- NullValue
- Format
- FormatProvider