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

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

このイベントを処理することによって、コントロール内のセルの外観をカスタマイズできます。セル全体を独自に描画したり、セルの一部分だけを描画したりできるほか、DataGridViewCellPaintingEventArgs.PaintBackground メソッドまたは DataGridViewCellPaintingEventArgs.PaintContent メソッドを使用して、その他の部分を描画することもできます。また、VisualStyleRenderer クラスを使用することにより、現在のテーマを使って標準コントロールを描画することもできます。詳細については、「visual スタイルが使用されているコントロールのレンダリング」を参照してください。Visual Studio 2005 を使用している場合は、豊富な標準イメージ ライブラリにアクセスし、DataGridView コントロールと組み合わせて使用することもできます。
詳細については、「 Visual Studio 2005 Image Library」を参照してください。
このイベントを処理する場合、セルに直接アクセスするのではなく、イベント ハンドラのパラメータを使用してセルにアクセスする必要があります。

このイベントを使用して、特定の列のすべてのセルの外観をカスタマイズする方法を次のコード例に示します。
ここに示すコードは、「方法 : Windows フォームの DataGridView コントロールのセルの外観をカスタマイズする」で取り上げられているコードの一部です。
Private Sub dataGridView1_CellPainting(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) _ Handles dataGridView1.CellPainting If Me.dataGridView1.Columns("ContactName").Index = _ e.ColumnIndex AndAlso e.RowIndex >= 0 Then Dim newRect As New Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, _ e.CellBounds.Width - 4, e.CellBounds.Height - 4) Dim backColorBrush As New SolidBrush(e.CellStyle.BackColor) Dim gridBrush As New SolidBrush(Me.dataGridView1.GridColor) Dim gridLinePen As New Pen(gridBrush) Try ' Erase the cell. e.Graphics.FillRectangle(backColorBrush, e.CellBounds) ' Draw the grid lines (only the right and bottom lines; ' DataGridView takes care of the others). e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, _ e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _ e.CellBounds.Bottom - 1) e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, _ e.CellBounds.Top, e.CellBounds.Right - 1, _ e.CellBounds.Bottom) ' Draw the inset highlight box. e.Graphics.DrawRectangle(Pens.Blue, newRect) ' Draw the text content of the cell, ignoring alignment. If Not (e.Value Is Nothing) Then e.Graphics.DrawString(CStr(e.Value), e.CellStyle.Font, _ Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, _ StringFormat.GenericDefault) End If e.Handled = True Finally gridLinePen.Dispose() gridBrush.Dispose() backColorBrush.Dispose() End Try End If End Sub
private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e) { if (this.dataGridView1.Columns["ContactName"].Index == e.ColumnIndex && e.RowIndex >= 0) { Rectangle newRect = new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 4, e.CellBounds.Height - 4); using ( Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor) , backColorBrush = new SolidBrush(e.CellStyle.BackColor)) { using (Pen gridLinePen = new Pen(gridBrush)) { // Erase the cell. e.Graphics.FillRectangle(backColorBrush, e.CellBounds); // Draw the grid lines (only the right and bottom lines; // DataGridView takes care of the others). e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); // Draw the inset highlight box. e.Graphics.DrawRectangle(Pens.Blue, newRect); // Draw the text content of the cell, ignoring alignment. if (e.Value != null) { e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault); } e.Handled = true; } } } }

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 名前空間
DataGridViewCellPaintingEventHandler
DataGridViewCellPaintingEventArgs
OnCellPainting
DataGridViewCell
その他の技術情報
方法 : Windows フォームの DataGridView コントロールのセルの外観をカスタマイズする
DataGridView コントロール (Windows フォーム)
Weblioに収録されているすべての辞書からDataGridView.CellPainting イベントを検索する場合は、下記のリンクをクリックしてください。

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