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

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

既定では、DataGridView コントロールはセルの値を表示に適した形式に変換しようとします。たとえば、数値をテキスト ボックス セルに表示するために文字列に変換します。使用する書式指定規約を指定するには、DefaultCellStyle プロパティなどのプロパティによって返された DataGridViewCellStyle の Format プロパティを設定します。
標準の書式指定が不十分な場合は、CellFormatting イベントを処理して書式指定をカスタマイズできます。このイベントにより、背景色と前景色など、セルの表示に使用する正確な表示値とセル スタイルの両方を指定できます。つまり、セルの値自体に書式指定が必要かどうかに関係なく、あらゆる種類のセル書式指定に対してこのイベントを処理できます。
CellFormatting イベントはセルが塗りつぶされるたびに発生するため、このイベントを処理するときは時間のかかる処理を回避する必要があります。このイベントは、セルの FormattedValue が取得された場合や、GetFormattedValue メソッドが呼び出された場合にも発生します。
CellFormatting イベントを処理するときに、ConvertEventArgs.Value プロパティはセルの値で初期化されます。セルの値から表示値へのカスタム変換を指定するには、ConvertEventArgs.Value プロパティを変換後の値に設定して、新しい値がセルの FormattedValueType プロパティで指定された型になるようにします。値の書式指定が不要になったことを指定するには、DataGridViewCellFormattingEventArgs.FormattingApplied プロパティを true に設定します。
イベント ハンドラが完了したときに、ConvertEventArgs.Value が null 参照 (Visual Basic では Nothing) になっているか、正しい型ではない場合、または DataGridViewCellFormattingEventArgs.FormattingApplied プロパティが false の場合、Value は、セルの InheritedStyle プロパティを使用して初期化される DataGridViewCellFormattingEventArgs.CellStyle プロパティによって返されたセル スタイルの Format、NullValue、DataSourceNullValue、および FormatProvider の各プロパティを使用して書式設定されます。
DataGridViewCellFormattingEventArgs.FormattingApplied プロパティの値に関係なく、セルの表示には、DataGridViewCellFormattingEventArgs.CellStyle プロパティによって返されるオブジェクトの表示プロパティが使用されます。
CellFormatting イベントによるカスタム書式指定の詳細については、「方法 : Windows フォーム DataGridView コントロールのデータの書式設定をカスタマイズする」を参照してください。
このイベントを処理するときにパフォーマンスが低下しないようにするには、セルに直接アクセスするのではなく、イベント ハンドラのパラメータを使用してセルにアクセスします。
書式設定されたユーザー指定の値を実際のセル値に変換するときにカスタマイズを適用するには、CellParsing イベントを処理します。

CellFormatting イベントを処理する方法を次のコード例に示します。
Private Sub dataGridView1_CellFormatting(ByVal sender As Object, _ ByVal e As DataGridViewCellFormattingEventArgs) _ Handles dataGridView1.CellFormatting ' If the column is the Artist column, check the ' value. If Me.dataGridView1.Columns(e.ColumnIndex).Name _ = "Artist" Then If e.Value IsNot Nothing Then ' Check for the string "pink" in the cell. Dim stringValue As String = _ CType(e.Value, String) stringValue = stringValue.ToLower() If ((stringValue.IndexOf("pink") > -1)) Then e.CellStyle.BackColor = Color.Pink End If End If ElseIf Me.dataGridView1.Columns(e.ColumnIndex).Name _ = "Release Date" Then ShortFormDateFormat(e) End If End Sub 'Even though the date internaly stores the year as YYYY, using formatting, the 'UI can have the format in YY. Private Shared Sub ShortFormDateFormat(ByVal formatting As DataGridViewCellFormattingEventArgs) If formatting.Value IsNot Nothing Then Try Dim dateString As System.Text.StringBuilder = New System.Text.StringBuilder() Dim theDate As Date = DateTime.Parse(formatting.Value.ToString()) dateString.Append(theDate.Month) dateString.Append("/") dateString.Append(theDate.Day) dateString.Append("/") dateString.Append(theDate.Year.ToString().Substring(2)) formatting.Value = dateString.ToString() formatting.FormattingApplied = True Catch notInDateFormat As FormatException ' Set to false in case there are other handlers interested trying to ' format this DataGridViewCellFormattingEventArgs instance. formatting.FormattingApplied = False End Try End If End Sub
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { // If the column is the Artist column, check the // value. if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Artist") { if (e.Value != null) { // Check for the string "pink" in the cell. string stringValue = (string)e.Value; stringValue = stringValue.ToLower(); if ((stringValue.IndexOf("pink") > -1)) { e.CellStyle.BackColor = Color.Pink; } } } else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Release Date") { ShortFormDateFormat(e); } } //Even though the date internaly stores the year as YYYY, using formatting, the //UI can have the format in YY. private static void ShortFormDateFormat(DataGridViewCellFormattingEventArgs formatting) { if (formatting.Value != null) { try { System.Text.StringBuilder dateString = new System.Text.StringBuilder(); DateTime theDate = DateTime.Parse(formatting.Value.ToString()); dateString.Append(theDate.Month); dateString.Append("/"); dateString.Append(theDate.Day); dateString.Append("/"); dateString.Append(theDate.Year.ToString().Substring(2)); formatting.Value = dateString.ToString(); formatting.FormattingApplied = true; } catch (FormatException) { // Set to false in case there are other handlers interested trying to // format this DataGridViewCellFormattingEventArgs instance. formatting.FormattingApplied = false; } } }
void dataGridView1_CellFormatting( Object^ /*sender*/, DataGridViewCellFormattingEventArgs^ e ) { // If the column is the Artist column, check the // value. if ( this->dataGridView1->Columns[ e->ColumnIndex ]->Name->Equals( "Artist" ) ) { if ( e->Value != nullptr ) { // Check for the string "pink" in the cell. String^ stringValue = dynamic_cast<String^>(e->Value); stringValue = stringValue->ToLower(); if ( (stringValue->IndexOf( "pink" ) > -1) ) { DataGridViewCellStyle^ pinkStyle = gcnew DataGridViewCellStyle; //Change the style of the cell. pinkStyle->BackColor = Color::Pink; pinkStyle->ForeColor = Color::Black; pinkStyle->Font = gcnew System::Drawing::Font( "Times New Roman",8,FontStyle::Bold ); e->CellStyle = pinkStyle; } } } else if ( this->dataGridView1->Columns[ e->ColumnIndex ]->Name->Equals( "Release Date" ) ) { ShortFormDateFormat( e ); } } //Even though the date internaly stores the year as YYYY, using formatting, the //UI can have the format in YY. void ShortFormDateFormat( DataGridViewCellFormattingEventArgs^ formatting ) { if ( formatting->Value != nullptr ) { try { System::Text::StringBuilder^ dateString = gcnew System::Text::StringBuilder; DateTime theDate = DateTime::Parse( formatting->Value->ToString() ); dateString->Append( theDate.Month ); dateString->Append( "/" ); dateString->Append( theDate.Day ); dateString->Append( "/" ); dateString->Append( theDate.Year.ToString()->Substring( 2 ) ); formatting->Value = dateString->ToString(); formatting->FormattingApplied = true; } catch ( Exception^ /*notInDateFormat*/ ) { // Set to false in case there are other handlers interested trying to // format this DataGridViewCellFormattingEventArgs instance. formatting->FormattingApplied = 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.DefaultCellStyle プロパティ
DataGridViewCellStyle
DataGridViewCellStyle.Format
DataGridViewCellStyle.FormatProvider
DataGridViewCellStyle.NullValue
DataGridViewCellStyle.DataSourceNullValue
DataGridViewCell.InheritedStyle
DataGridViewCell.Value
DataGridViewCell.FormattedValue
DataGridViewCell.FormattedValueType
DataGridViewCell.GetFormattedValue
DataGridViewCellFormattingEventHandler
DataGridViewCellFormattingEventArgs
DataGridViewCellFormattingEventArgs.FormattingApplied
DataGridViewCellFormattingEventArgs.CellStyle
ConvertEventArgs.Value プロパティ
OnCellFormatting
CellParsing
その他の技術情報
Windows フォーム DataGridView コントロールでのセルのスタイル
方法 : Windows フォーム DataGridView コントロールのデータの書式設定をカスタマイズする
Windows フォーム DataGridView コントロールでのセルのスタイル
DataGridView コントロール (Windows フォーム)
Weblioに収録されているすべての辞書からDataGridView.CellFormatting イベントを検索する場合は、下記のリンクをクリックしてください。

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