DataGridView.RowPrePaint 事件

定义

在绘制 DataGridViewRow 前发生。

C#
public event System.Windows.Forms.DataGridViewRowPrePaintEventHandler RowPrePaint;
C#
public event System.Windows.Forms.DataGridViewRowPrePaintEventHandler? RowPrePaint;

事件类型

示例

下面的代码示例演示如何使用 事件的处理程序来 RowPrePaint 绘制渐变行背景(如果选择了该行)。 此示例是如何:自定义 DataGridView 控件中行的外观中提供的更大示例的 Windows 窗体一部分。

C#
// 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);
        }
    }
}

注解

可以单独处理此事件,也可以与 RowPostPaint 事件结合使用,以自定义控件中行的外观。 可以自己绘制整行,也可以绘制行的特定部分,并使用 类的 DataGridViewRowPrePaintEventArgs 以下方法来绘制其他部分:

还可以使用 VisualStyleRenderer 类使用当前主题绘制标准控件。 有关详细信息,请参阅使用视觉样式呈现控件。 如果使用 Visual Studio 2005,还可以访问可与 控件一起使用 DataGridView 的大型标准图像库。

有关如何处理事件的详细信息,请参阅 处理和引发事件

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅