Language

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 プロパティによって返されるオブジェクトのDataSourceNullValue、FormatProvider、およびDataGridViewCellParsingEventArgs.InheritedCellStyleプロパティを変更することもできます。このプロパティは、セル InheritedStyle プロパティを使用して初期化されます。

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

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

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

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

適用対象

こちらもご覧ください