DataGridViewRowPostPaintEventArgs DataGridViewRowPostPaintEventArgs DataGridViewRowPostPaintEventArgs DataGridViewRowPostPaintEventArgs Class

定义

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

public ref class DataGridViewRowPostPaintEventArgs : EventArgs
public class DataGridViewRowPostPaintEventArgs : EventArgs
type DataGridViewRowPostPaintEventArgs = class
    inherit EventArgs
Public Class DataGridViewRowPostPaintEventArgs
Inherits EventArgs
继承
DataGridViewRowPostPaintEventArgsDataGridViewRowPostPaintEventArgsDataGridViewRowPostPaintEventArgsDataGridViewRowPostPaintEventArgs

示例

下面的代码示例演示如何处理RowPostPaint事件, 使单元格内容跨越整个行。The following code example demonstrates how to handle the RowPostPaint event to make the content of a cell span the entire row. 此代码示例摘自了如何:自定义 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 content that spans multiple columns and the focus rectangle.
void dataGridView1_RowPostPaint(object sender,
    DataGridViewRowPostPaintEventArgs e)
{
    // 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);

    SolidBrush forebrush = null;
    try
    {
        // Determine the foreground color.
        if ((e.State & DataGridViewElementStates.Selected) ==
            DataGridViewElementStates.Selected)
        {
            forebrush = new SolidBrush(e.InheritedRowStyle.SelectionForeColor);
        }
        else
        {
            forebrush = new SolidBrush(e.InheritedRowStyle.ForeColor);
        }

        // Get the content that spans multiple columns.
        object recipe =
            this.dataGridView1.Rows.SharedRow(e.RowIndex).Cells[2].Value;

        if (recipe != null)
        {
            String text = recipe.ToString();

            // Calculate the bounds for the content that spans multiple 
            // columns, adjusting for the horizontal scrolling position 
            // and the current row height, and displaying only whole
            // lines of text.
            Rectangle textArea = rowBounds;
            textArea.X -= this.dataGridView1.HorizontalScrollingOffset;
            textArea.Width += this.dataGridView1.HorizontalScrollingOffset;
            textArea.Y += rowBounds.Height - e.InheritedRowStyle.Padding.Bottom;
            textArea.Height -= rowBounds.Height -
                e.InheritedRowStyle.Padding.Bottom;
            textArea.Height = (textArea.Height / e.InheritedRowStyle.Font.Height) *
                e.InheritedRowStyle.Font.Height;

            // Calculate the portion of the text area that needs painting.
            RectangleF clip = textArea;
            clip.Width -= this.dataGridView1.RowHeadersWidth + 1 - clip.X;
            clip.X = this.dataGridView1.RowHeadersWidth + 1;
            RectangleF oldClip = e.Graphics.ClipBounds;
            e.Graphics.SetClip(clip);

            // Draw the content that spans multiple columns.
            e.Graphics.DrawString(
                text, e.InheritedRowStyle.Font, forebrush, textArea);

            e.Graphics.SetClip(oldClip);
        }
    }
    finally
    {
        forebrush.Dispose();
    }

    if (this.dataGridView1.CurrentCellAddress.Y == e.RowIndex)
    {
        // Paint the focus rectangle.
        e.DrawFocus(rowBounds, true);
    }
}
' Paints the content that spans multiple columns and the focus rectangle.
Sub dataGridView1_RowPostPaint(ByVal sender As Object, _
    ByVal e As DataGridViewRowPostPaintEventArgs) _
    Handles dataGridView1.RowPostPaint

    ' 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)

    Dim forebrush As SolidBrush = Nothing
    Try
        ' Determine the foreground color.
        If (e.State And DataGridViewElementStates.Selected) = _
            DataGridViewElementStates.Selected Then

            forebrush = New SolidBrush(e.InheritedRowStyle.SelectionForeColor)
        Else
            forebrush = New SolidBrush(e.InheritedRowStyle.ForeColor)
        End If

        ' Get the content that spans multiple columns.
        Dim recipe As Object = _
            Me.dataGridView1.Rows.SharedRow(e.RowIndex).Cells(2).Value

        If (recipe IsNot Nothing) Then
            Dim text As String = recipe.ToString()

            ' Calculate the bounds for the content that spans multiple 
            ' columns, adjusting for the horizontal scrolling position 
            ' and the current row height, and displaying only whole
            ' lines of text.
            Dim textArea As Rectangle = rowBounds
            textArea.X -= Me.dataGridView1.HorizontalScrollingOffset
            textArea.Width += Me.dataGridView1.HorizontalScrollingOffset
            textArea.Y += rowBounds.Height - e.InheritedRowStyle.Padding.Bottom
            textArea.Height -= rowBounds.Height - e.InheritedRowStyle.Padding.Bottom
            textArea.Height = (textArea.Height \ e.InheritedRowStyle.Font.Height) * _
                e.InheritedRowStyle.Font.Height

            ' Calculate the portion of the text area that needs painting.
            Dim clip As RectangleF = textArea
            clip.Width -= Me.dataGridView1.RowHeadersWidth + 1 - clip.X
            clip.X = Me.dataGridView1.RowHeadersWidth + 1
            Dim oldClip As RectangleF = e.Graphics.ClipBounds
            e.Graphics.SetClip(clip)

            ' Draw the content that spans multiple columns.
            e.Graphics.DrawString(text, e.InheritedRowStyle.Font, forebrush, _
                textArea)

            e.Graphics.SetClip(oldClip)
        End If
    Finally
        forebrush.Dispose()
    End Try

    If Me.dataGridView1.CurrentCellAddress.Y = e.RowIndex Then
        ' Paint the focus rectangle.
        e.DrawFocus(rowBounds, True)
    End If

End Sub

注解

RowPostPaint 控件DataGridView上绘制行后发生该事件。The RowPostPaint event occurs after a row is painted on a DataGridView control. RowPostPaint使您能够在绘制行中的单元格之后手动调整行的外观。RowPostPaint allows you to manually adjust the appearance of the row after the cells in the row are painted. 如果要自定义行, 这会很有用。This is useful if you want to customize the row.

构造函数

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

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

属性

ClipBounds ClipBounds ClipBounds ClipBounds

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

ErrorText ErrorText ErrorText ErrorText

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

Graphics Graphics Graphics Graphics

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

InheritedRowStyle InheritedRowStyle InheritedRowStyle InheritedRowStyle

获取应用于当前 DataGridViewRow 的单元格样式。Gets the cell style applied to the current DataGridViewRow.

IsFirstDisplayedRow IsFirstDisplayedRow IsFirstDisplayedRow IsFirstDisplayedRow

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

IsLastVisibleRow IsLastVisibleRow IsLastVisibleRow IsLastVisibleRow

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

RowBounds RowBounds RowBounds RowBounds

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

RowIndex RowIndex RowIndex RowIndex

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

State State State State

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

方法

DrawFocus(Rectangle, Boolean) DrawFocus(Rectangle, Boolean) DrawFocus(Rectangle, Boolean) DrawFocus(Rectangle, Boolean)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
PaintCells(Rectangle, DataGridViewPaintParts) PaintCells(Rectangle, DataGridViewPaintParts) PaintCells(Rectangle, DataGridViewPaintParts) PaintCells(Rectangle, DataGridViewPaintParts)

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

PaintCellsBackground(Rectangle, Boolean) PaintCellsBackground(Rectangle, Boolean) PaintCellsBackground(Rectangle, Boolean) PaintCellsBackground(Rectangle, Boolean)

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

PaintCellsContent(Rectangle) PaintCellsContent(Rectangle) PaintCellsContent(Rectangle) PaintCellsContent(Rectangle)

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

PaintHeader(Boolean) PaintHeader(Boolean) PaintHeader(Boolean) PaintHeader(Boolean)

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

PaintHeader(DataGridViewPaintParts) PaintHeader(DataGridViewPaintParts) PaintHeader(DataGridViewPaintParts) PaintHeader(DataGridViewPaintParts)

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

ToString() ToString() ToString() ToString()

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

(Inherited from Object)

适用于

另请参阅