DataGridViewRowPrePaintEventArgs 类

定义

RowPrePaint 事件提供数据。Provides data for the RowPrePaint event.

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事件以绘制所选单元格的自定义背景。The following code example demonstrates how to handle the RowPrePaint event to draw a custom background for selected cells. 此代码示例摘自了如何:自定义 Windows 窗体 DataGridView 控件中的行的外观。This code example is part of a larger example provided 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) ==
                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

注解

事件在DataGridView控件上绘制行之前发生。 RowPrePaintThe RowPrePaint event occurs before a row is painted on a DataGridView control. RowPrePaint使您能够在绘制行中的任何单元格之前手动调整行的外观。RowPrePaint enables you to manually adjust the appearance of the row before any of the cells in the row are painted. 如果要自定义行 (如) 以生成一个列的内容跨多个列的行, 则此方法非常有用。This is useful if you want to customize the row, such as to produce a row where the content of one column spans multiple columns. 使用中DataGridViewRowPrePaintEventArgs的属性可获取行的设置, 而无需直接访问DataGridView中的行。Use the properties in DataGridViewRowPrePaintEventArgs to get the settings of the row without directly accessing the row in the DataGridView.

构造函数

DataGridViewRowPrePaintEventArgs(DataGridView, Graphics, Rectangle, Rectangle, Int32, DataGridViewElementStates, String, DataGridViewCellStyle, Boolean, Boolean)

初始化 DataGridViewRowPrePaintEventArgs 类的新实例。Initializes a new instance of the DataGridViewRowPrePaintEventArgs class.

属性

ClipBounds

获取或设置需要重新绘制的 DataGridView 区域。Gets or sets the area of the DataGridView that needs to be repainted.

ErrorText

获取表示当前 DataGridViewRow 的错误消息的字符串。Gets a string that represents an error message for the current DataGridViewRow.

Graphics

获取用于绘制当前 GraphicsDataGridViewRowGets the Graphics used to paint the current DataGridViewRow.

Handled

获取或设置一个值,该值指示事件处理程序是否已完整处理事件,或者系统是否应该继续本身的处理。Gets or sets a value that indicates whether the event handler has completely handled the event or whether the system should continue its own processing.

(继承自 HandledEventArgs)
InheritedRowStyle

获取应用于行的单元格样式。Gets the cell style applied to the row.

IsFirstDisplayedRow

获取一个指示当前行是否为 DataGridView 中当前显示的第一行的值。Gets a value indicating whether the current row is the first row currently displayed in the DataGridView.

IsLastVisibleRow

获取一个指示当前行是否为 DataGridView 中最后一个可见行的值。Gets a value indicating whether the current row is the last visible row in the DataGridView.

PaintParts

要绘制的单元格部分。The cell parts that are to be painted.

RowBounds

获取当前 DataGridViewRow 的边界。Get the bounds of the current DataGridViewRow.

RowIndex

获取当前 DataGridViewRow 的索引。Gets the index of the current DataGridViewRow.

State

获取当前 DataGridViewRow 的状态。Gets the state of the current DataGridViewRow.

方法

DrawFocus(Rectangle, Boolean)

围绕指定的边界绘制聚焦框。Draws the focus rectangle around the specified bounds.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

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

为指定边界中的区域绘制指定的单元格部分。Paints the specified cell parts for the area in the specified bounds.

PaintCellsBackground(Rectangle, Boolean)

为指定边界中的区域绘制单元格背景。Paints the cell backgrounds for the area in the specified bounds.

PaintCellsContent(Rectangle)

为指定边界中的区域绘制单元格内容。Paints the cell contents for the area in the specified bounds.

PaintHeader(Boolean)

绘制当前 DataGridViewRow 的整个行标头。Paints the entire row header of the current DataGridViewRow.

PaintHeader(DataGridViewPaintParts)

绘制当前行的行标头的指定部分。Paints the specified parts of the row header of the current row.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅