DataGridViewCellParsingEventArgs 類別

定義

提供 CellParsing 控制項的 DataGridView 事件的資料。

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
繼承
DataGridViewCellParsingEventArgs

範例

下列程式碼範例示範如何使用 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 傳回物件的 、 DataSourceNullValueFormatProvider 屬性,該屬性會使用儲存格 InheritedStyle 屬性初始化。

當您自行轉換值時,請將 屬性的初始格式值 ConvertEventArgs.Value 取代為儲存格 ValueType 屬性所指定型別中的已轉換值。 若要指出不需要進一步剖析,請將 DataGridViewCellParsingEventArgs.ParsingApplied 屬性設定為 true

當事件處理常式完成時,如果 Valuenull 或 不是正確的型別,或 ParsingApplied 屬性為 false ,則會 Value 使用具有預設類型轉換子的儲存格 ParseFormattedValue 方法剖析 。 此方法的預設實作會使用 NullValue 傳入之儲存格樣式的 、 DataSourceNullValueFormatProvider 屬性來剖析值。 如果值不等於 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)

適用於

另請參閱