DataGridView.CellParsing イベント
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As DataGridView Dim handler As DataGridViewCellParsingEventHandler AddHandler instance.CellParsing, handler
public: event DataGridViewCellParsingEventHandler^ CellParsing { void add (DataGridViewCellParsingEventHandler^ value); void remove (DataGridViewCellParsingEventHandler^ value); }

既定では、DataGridView コントロールは、セルに表示されるユーザー指定の値を、セルの ValueType プロパティで指定された型の実際の基になるセル値に変換しようとします。この変換は、セルの InheritedStyle プロパティによって返されたセル スタイルの書式指定プロパティを使用します。
標準変換が要件を満たしていない場合は、CellParsing イベントを処理して、必要な型へのカスタム値変換を指定します。
ユーザーは、EditMode プロパティで指定されたメソッドを使用して編集モードを開始し、別のセルに移動するか、Enter キーを押して、編集モードを終了し、変更をセルにコミットします。Esc キーを押すと、変更がコミット前の値に戻り、CellParsing イベントは発生しません。CellParsing イベントは、最後の値が元の値と同じであっても、セルの値が実際に変更された場合にだけ発生します。CommitEdit メソッドが呼び出されたときにも発生します。
CellParsing イベントを処理する場合、値を自分で変換するか、既定の変換をカスタマイズできます。たとえば、セルの ParseFormattedValue メソッドと選択した型コンバータを使用して、値を自分で変更できます。または、既定の型コンバータに値を解析させる一方で、セルの InheritedStyle プロパティを使用して初期化される DataGridViewCellParsingEventArgs.InheritedCellStyle プロパティによって返されたオブジェクトの NullValue、DataSourceNullValue、および FormatProvider の各プロパティを変更できます。
値を自分で変換する場合は、ConvertEventArgs.Value プロパティの書式設定された最初の値を、セルの ValueType プロパティで指定された型の変換後の値に置換します。解析が不要になったことを指定するには、DataGridViewCellParsingEventArgs.ParsingApplied プロパティを true に設定します。
イベント ハンドラが完了したときに、ConvertEventArgs.Value が null 参照 (Visual Basic では Nothing) になっているか、正しい型ではない場合、または DataGridViewCellParsingEventArgs.ParsingApplied プロパティが false の場合、Value は、セルの ParseFormattedValue メソッドと既定の型コンバータを使用して解析されます。このメソッドの既定の実装は、渡されたセル スタイルの NullValue、DataSourceNullValue、および FormatProvider の各プロパティを使用して値を解析します。値が NullValue でない場合、その値は、渡された FormatProvider プロパティと型コンバータを使用して解析されます。
セル値を、表示に合わせて書式設定された値に変換するときにカスタマイズを行うには、CellFormatting イベントを処理します。

CellParsing イベントを処理する方法を次のコード例に示します。この例では、DataGridViewCellParsingEventArgs クラスの使用方法も示します。
' 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
// 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. 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; } } } } }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


DataGridView クラス
DataGridView メンバ
System.Windows.Forms 名前空間
DataGridView.CellParsing イベント
OnCellParsing
CommitEdit
DataGridView.CellFormatting イベント
DataGridView.EditMode プロパティ
DataGridViewCell.ValueType
DataGridViewCell.InheritedStyle
DataGridViewCell.ParseFormattedValue
DataGridViewCellParsingEventHandler
DataGridViewCellParsingEventArgs
DataGridViewCellParsingEventArgs.InheritedCellStyle
DataGridViewCellParsingEventArgs.ParsingApplied
ConvertEventArgs.Value プロパティ
DataGridViewCellStyle
DataGridViewCellStyle.NullValue
DataGridViewCellStyle.Format
DataGridViewCellStyle.FormatProvider
その他の技術情報
Windows フォーム DataGridView コントロールでのセルのスタイル
DataGridView コントロール (Windows フォーム)
Weblioに収録されているすべての辞書からDataGridView.CellParsing イベントを検索する場合は、下記のリンクをクリックしてください。

- DataGridView.CellParsing イベントのページへのリンク