DataGridView.RowPrePaint イベント

定義

DataGridViewRowが描画される前に発生します。

public:
 event System::Windows::Forms::DataGridViewRowPrePaintEventHandler ^ RowPrePaint;
public event System.Windows.Forms.DataGridViewRowPrePaintEventHandler RowPrePaint;
member this.RowPrePaint : System.Windows.Forms.DataGridViewRowPrePaintEventHandler 
Public Custom Event RowPrePaint As DataGridViewRowPrePaintEventHandler 

イベントの種類

次のコード例では、 RowPrePaint イベントのハンドラーを使用して、行が選択されている場合にグラデーション行の背景を描画する方法を示します。 この例は、「 方法: Windows フォーム DataGridView コントロールの行の外観をカスタマイズする」で使用できるより大きな例の一部です。

// Paints the custom selection background for selected rows.
void dataGridView1_RowPrePaint(object sender,
        DataGridViewRowPrePaintEventArgs e)
{
    // Do not automatically paint the focus rectangle.
    e.PaintParts &= ~DataGridViewPaintParts.Focus;

    // Determine whether the cell should be painted
    // with the custom selection background.
    if ((e.State & DataGridViewElementStates.Selected) ==
                DataGridViewElementStates.Selected)
    {
        // Calculate the bounds of the row.
        Rectangle rowBounds = new Rectangle(
            this.dataGridView1.RowHeadersWidth, e.RowBounds.Top,
            this.dataGridView1.Columns.GetColumnsWidth(
                DataGridViewElementStates.Visible) -
            this.dataGridView1.HorizontalScrollingOffset + 1,
            e.RowBounds.Height);

        // Paint the custom selection background.
        using (Brush backbrush =
            new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
                this.dataGridView1.DefaultCellStyle.SelectionBackColor,
                e.InheritedRowStyle.ForeColor,
                System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
        {
            e.Graphics.FillRectangle(backbrush, rowBounds);
        }
    }
}
' Paints the custom selection background for selected rows.
Sub dataGridView1_RowPrePaint(ByVal sender As Object, _
    ByVal e As DataGridViewRowPrePaintEventArgs) _
    Handles dataGridView1.RowPrePaint

    ' Do not automatically paint the focus rectangle.
    e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Focus

    ' Determine whether the cell should be painted with the 
    ' custom selection background.
    If (e.State And DataGridViewElementStates.Selected) = _
        DataGridViewElementStates.Selected Then

        ' Calculate the bounds of the row.
        Dim rowBounds As New Rectangle( _
            Me.dataGridView1.RowHeadersWidth, e.RowBounds.Top, _
            Me.dataGridView1.Columns.GetColumnsWidth( _
            DataGridViewElementStates.Visible) - _
            Me.dataGridView1.HorizontalScrollingOffset + 1, _
            e.RowBounds.Height)

        ' Paint the custom selection background.
        Dim backbrush As New _
            System.Drawing.Drawing2D.LinearGradientBrush(rowBounds, _
            Me.dataGridView1.DefaultCellStyle.SelectionBackColor, _
            e.InheritedRowStyle.ForeColor, _
            System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
        Try
            e.Graphics.FillRectangle(backbrush, rowBounds)
        Finally
            backbrush.Dispose()
        End Try
    End If

End Sub

注釈

このイベントは、単独で、または RowPostPaint イベントと組み合わせて処理して、コントロール内の行の外観をカスタマイズできます。 行全体を自分で塗りつぶしたり、行の特定の部分を塗りつぶしたり、 DataGridViewRowPrePaintEventArgs クラスの次のメソッドを使用して他の部分を描画したりできます。

VisualStyleRenderer クラスを使用して、現在のテーマを使用して標準コントロールを描画することもできます。 詳細については、「 表示スタイルを使用したコントロールのレンダリング」を参照してください。 Visual Studio 2005 を使用している場合は、DataGridView コントロールで使用できる標準イメージの大規模なライブラリにもアクセスできます。

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

適用対象

こちらもご覧ください