DataGridViewRowPrePaintEventArgs 类

定义

提供事件的数据 RowPrePaint

public ref class DataGridViewRowPrePaintEventArgs : System::ComponentModel::HandledEventArgs
public class DataGridViewRowPrePaintEventArgs : System.ComponentModel.HandledEventArgs
type DataGridViewRowPrePaintEventArgs = class
    inherit HandledEventArgs
Public Class DataGridViewRowPrePaintEventArgs
Inherits HandledEventArgs
继承
DataGridViewRowPrePaintEventArgs

示例

下面的代码示例演示如何处理 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

注解

RowPrePaint 控件上 DataGridView 绘制行之前发生该事件。 RowPrePaint 使你能够在绘制行中的任何单元格之前手动调整行的外观。 如果要自定义行,例如生成一个行,其中一列的内容跨越多个列,这非常有用。 使用 中的DataGridViewRowPrePaintEventArgs属性获取行的设置,而无需直接访问行。DataGridView

构造函数

名称 说明
DataGridViewRowPrePaintEventArgs(DataGridView, Graphics, Rectangle, Rectangle, Int32, DataGridViewElementStates, String, DataGridViewCellStyle, Boolean, Boolean)

初始化 DataGridViewRowPrePaintEventArgs 类的新实例。

属性

名称 说明
ClipBounds

获取或设置需要重新绘制的区域 DataGridView

ErrorText

获取表示当前 DataGridViewRow错误消息的字符串。

Graphics

Graphics获取用于绘制当前 DataGridViewRow

Handled

获取或设置一个值,该值指示事件处理程序是否已完全处理该事件,或者系统是否应继续其自己的处理。

(继承自 HandledEventArgs)
InheritedRowStyle

获取应用于行的单元格样式。

IsFirstDisplayedRow

获取一个值,该值指示当前行是否是当前显示在其中 DataGridView的第一行。

IsLastVisibleRow

获取一个值,该值指示当前行是否为中 DataGridView最后一个可见行。

PaintParts

要绘制的单元格部件。

RowBounds

获取当前 DataGridViewRow边界。

RowIndex

获取当前 DataGridViewRow索引。

State

获取当前 DataGridViewRow状态。

方法

名称 说明
DrawFocus(Rectangle, Boolean)

在指定的边界周围绘制焦点矩形。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
PaintCells(Rectangle, DataGridViewPaintParts)

绘制指定边界中区域的指定单元格部分。

PaintCellsBackground(Rectangle, Boolean)

绘制指定边界中区域的单元格背景。

PaintCellsContent(Rectangle)

绘制指定边界中的区域的单元格内容。

PaintHeader(Boolean)

绘制当前 DataGridViewRow行的整个行标题。

PaintHeader(DataGridViewPaintParts)

绘制当前行的行标题的指定部分。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅