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 コントロールは、セルに表示されるユーザー指定の値を、cell プロパティで指定された ValueType 型の実際の基になるセル値に変換しようとします。 この変換では、セル プロパティによって返されるセル スタイルの書式設定プロパティを InheritedStyle 使用します。

標準変換がニーズを満たさない場合は、 イベントを CellParsing 処理して、必要な型へのカスタム値変換を提供します。

ユーザーは、 プロパティで指定されたメソッドを使用して編集モードに EditMode 入ることができ、編集モードを切り替えたり、セルに変更をコミットしたり、別のセルに移動したり、Enter キーを押したりすることができます。 ESC キーを押すと、コミットされる前に値に対する変更が元に CellParsing 戻され、イベントは発生しません。 イベントは CellParsing 、最終的な値が元の値と同じであっても、セル値が実際に変更された場合にのみ発生します。 また、 メソッドが呼び出されたときに CommitEdit も発生します。

イベントを CellParsing 処理するときは、値を自分で変換することも、既定の変換をカスタマイズすることもできます。 たとえば、選択した型コンバーターを使用して cell ParseFormattedValue メソッドを使用して、自分で値を変換できます。 または、既定の型コンバーターで値を解析させ、セル InheritedStyle プロパティを使用して初期化される プロパティによってDataGridViewCellParsingEventArgs.InheritedCellStyle返されるオブジェクトの 、DataSourceNullValue、および FormatProvider プロパティを変更NullValueすることもできます。

値を自分で変換するときは、プロパティの最初の書式設定された値を ConvertEventArgs.Value 、セル ValueType プロパティで指定された型の変換された値に置き換えます。 これ以上解析する必要がないことを示すには、 プロパティを DataGridViewCellParsingEventArgs.ParsingAppliedtrue設定します。

イベント ハンドラーが完了すると、 がnull正しい型であるか、または プロパティfalseValueが の場合ConvertEventArgs.Valueは、 が既定の型DataGridViewCellParsingEventArgs.ParsingAppliedコンバーターを使用してセル ParseFormattedValue メソッドを使用して解析されます。 このメソッドの既定の実装では、渡されたセル スタイルの NullValueDataSourceNullValue、および FormatProvider プロパティを使用して値を解析します。 値が と NullValue等しくない場合、値は プロパティと渡された型コンバーターを使用して FormatProvider 解析されます。

セル値を表示用の書式設定された値に変換するようにカスタマイズするには、 イベントを処理します CellFormatting

イベントを処理する方法の詳細については、次を参照してください。処理とイベントの発生します。

適用対象

こちらもご覧ください