DataGridViewRowPostPaintEventArgs 類別

定義

提供 RowPostPaint 事件的資料。

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

範例

下列程式碼範例示範如何處理 RowPostPaint 事件,使儲存格的內容跨越整個資料列。 此程式碼範例是如何:自訂 DataGridView 控制項中 Windows Forms資料列外觀中提供之較大範例的一部分。

// 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

備註

在控制項上 DataGridView 繪製資料列之後,就會發生此 RowPostPaint 事件。 RowPostPaint 可讓您在繪製資料列的儲存格之後,手動調整資料列的外觀。 如果您想要自訂資料列,這會很有用。

建構函式

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

初始化 DataGridViewRowPostPaintEventArgs 類別的新執行個體。

屬性

ClipBounds

取得或設定需要重新繪製的 DataGridView 區域。

ErrorText

取得字串,表示目前 DataGridViewRow 的錯誤訊息。

Graphics

取得用來繪製目前 GraphicsDataGridViewRow

InheritedRowStyle

取得套用至目前 DataGridViewRow 的儲存格樣式。

IsFirstDisplayedRow

取得值,指出目前的資料列是否為 DataGridView 中所顯示的第一個資料列。

IsLastVisibleRow

取得值,指出目前的資料列是否為 DataGridView 中所顯示的最後一個資料列。

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)

適用於

另請參閱