共用方式為


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 事件,將使用者指定的值轉換為 cell 屬性指定的 ValueType 值,提供自訂的值轉換。

當你處理事件時 CellParsing ,你可以自己轉換價值,或是自訂預設轉換。 例如,你可以自己用 ParseFormattedValue 單元法轉換值,搭配你選擇的型別轉換器。 或者,你可以讓預設型別的轉換器解析該值,但修改NullValue屬性回傳DataGridViewCellParsingEventArgs.InheritedCellStyle物件的 、 DataSourceNullValue、 和FormatProvider屬性,該屬性是用 cell InheritedStyle 屬性初始化的。

當你自己轉換值時,將屬性的初始格式化值 ConvertEventArgs.Value 替換成儲存格 ValueType 屬性指定的轉換值。 為表示無需進一步解析,將屬性設 DataGridViewCellParsingEventArgs.ParsingAppliedtrue

當事件處理程序完成時,如果 是Valuenull或不是正確型別,或屬性ParsingAppliedfalse,則Value會使用帶有預設型別轉換器的單元ParseFormattedValue法進行解析。 此方法的預設實作會利用 NullValue輸入的儲存格樣式的 、 、 DataSourceNullValueFormatProvider 屬性來解析該值。 若值不等於 NullValue,則會利用 FormatProvider 屬性解析,並傳遞入的型別轉換器。

若要自訂將儲存格值轉換為格式化值以供顯示,請處理事件。CellFormatting

欲了解更多如何處理事件的資訊,請參閱 「處理與提升事件」。

建構函式

名稱 Description
DataGridViewCellParsingEventArgs(Int32, Int32, Object, Type, DataGridViewCellStyle)

初始化 DataGridViewCellParsingEventArgs 類別的新執行個體。

屬性

名稱 Description
ColumnIndex

取得需要解析的儲存格資料的欄位索引。

DesiredType

取得所需值的資料型態。

(繼承來源 ConvertEventArgs)
InheritedCellStyle

取得或設定套用到編輯儲存格的樣式。

ParsingApplied

取得或設定一個值,表示儲存格值是否已被成功解析。

RowIndex

取得需要解析的儲存格的列索引。

Value

取得或設定 的 ConvertEventArgs值。

(繼承來源 ConvertEventArgs)

方法

名稱 Description
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於

另請參閱