DataGridView.RowPrePaint 事件


在绘制 DataGridViewRow 前发生。Occurs before a DataGridViewRow is painted.

 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 事件的处理程序来绘制渐变行背景(如果选中该行)。The following code example demonstrates how to use a handler for the RowPrePaint event to paint a gradient row background if the row is selected. 此示例摘自如何:自定义 Windows 窗体 DataGridView 控件中行的外观的更大示例的一部分。This example is part of a larger example available in How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control.

// 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) ==
        // Calculate the bounds of the row.
        Rectangle rowBounds = new Rectangle(
            this.dataGridView1.RowHeadersWidth, e.RowBounds.Top,
                DataGridViewElementStates.Visible) -
            this.dataGridView1.HorizontalScrollingOffset + 1,

        // Paint the custom selection background.
        using (Brush backbrush =
            new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
            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, _

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

End Sub


您可以单独处理此事件,也可以结合 RowPostPaint 事件来自定义控件中行的外观。You can handle this event alone or in combination with the RowPostPaint event to customize the appearance of rows in the control. 您可以自行绘制整行,或绘制行的特定部分,并使用 DataGridViewRowPrePaintEventArgs 类的以下方法来绘制其他部分:You can paint entire rows yourself, or paint specific parts of rows and use the following methods of the DataGridViewRowPrePaintEventArgs class to paint other parts:

你还可以使用 VisualStyleRenderer 类来使用当前主题绘制标准控件。You can also use the VisualStyleRenderer class to paint standard controls using the current theme. 有关详细信息,请参阅使用视觉样式呈现控件For more information, see Rendering Controls with Visual Styles. 如果使用 Visual Studio 2005Visual Studio 2005,则还可以访问可用于 DataGridView 控件的大型标准图像库。If you are using Visual Studio 2005Visual Studio 2005, you also have access to a large library of standard images that you can use with the DataGridView control.

有关如何处理事件的详细信息,请参阅处理和引发事件For more information about how to handle events, see Handling and Raising Events.