DataGridView.CellParsing 事件

定義

發生於已經修改儲存格值後,該儲存格離開編輯模式時。

public:
 event System::Windows::Forms::DataGridViewCellParsingEventHandler ^ CellParsing;
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 事件。 它也會示範如何使用 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

備註

根據預設, DataGridView 控制項會嘗試將儲存格中顯示的使用者指定值轉換成儲存格 ValueType 屬性所指定類型中的實際基礎儲存格值。 此轉換會使用儲存格屬性所傳回之儲存格 InheritedStyle 樣式的格式屬性。

如果標準轉換不符合您的需求,請處理 CellParsing 事件,以提供自訂值轉換至所需的類型。

使用者可以使用 屬性所 EditMode 指定的方法來進入編輯模式,也可以保留編輯模式、認可儲存格的任何變更、移至另一個儲存格或按 ENTER 鍵。 按下 ESC 會在認可值之前還原任何變更,而且 CellParsing 不會發生事件。 CellParsing只有在實際修改儲存格值時,才會發生此事件,即使最終值與原始值相同也一樣。 呼叫 方法時 CommitEdit 也會發生這種情況。

當您處理 CellParsing 事件時,您可以自行轉換值,也可以自訂預設轉換。 例如,您可以使用儲存格方法搭配您選擇的類型轉換器,自行 ParseFormattedValue 轉換值。 或者,您可以讓預設類型轉換器剖析值,但修改 NullValue 屬性所 DataGridViewCellParsingEventArgs.InheritedCellStyle 傳回之物件的 、 DataSourceNullValueFormatProvider 屬性,該屬性是使用儲存格 InheritedStyle 屬性初始化。

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

當事件處理常式完成時,如果 ConvertEventArgs.Valuenull 或 不是正確的型別,或 DataGridViewCellParsingEventArgs.ParsingApplied 屬性為 falseValue 則會使用具有預設類型轉換器的儲存格 ParseFormattedValue 方法剖析 。 這個方法的預設實作會使用傳入之儲存格樣式的 NullValueDataSourceNullValueFormatProvider 屬性來剖析值。 如果值不等於 NullValue ,則會使用 FormatProvider 屬性和傳入的類型轉換器來剖析值。

若要自訂儲存格值轉換成要顯示的格式化值,請處理 CellFormatting 事件。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱