Edit

Share via


DataGridViewRowPostPaintEventArgs Class

Definition

Provides data for the RowPostPaint event.

C#
public class DataGridViewRowPostPaintEventArgs : EventArgs
Inheritance
DataGridViewRowPostPaintEventArgs

Examples

The following code example demonstrates how to handle the RowPostPaint event to make the content of a cell span the entire row. This code example is part of a larger example provided in How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control.

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

Remarks

The RowPostPaint event occurs after a row is painted on a DataGridView control. 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.

Constructors

Properties

ClipBounds

Gets or sets the area of the DataGridView that needs to be repainted.

ErrorText

Gets a string that represents an error message for the current DataGridViewRow.

Graphics

Gets the Graphics used to paint the current DataGridViewRow.

InheritedRowStyle

Gets the cell style applied to the current DataGridViewRow.

IsFirstDisplayedRow

Gets a value indicating whether the current row is the first row displayed in the DataGridView.

IsLastVisibleRow

Gets a value indicating whether the current row is the last visible row displayed in the DataGridView.

RowBounds

Get the bounds of the current DataGridViewRow.

RowIndex

Gets the index of the current DataGridViewRow.

State

Gets the state of the current DataGridViewRow.

Methods

DrawFocus(Rectangle, Boolean)

Draws the focus rectangle around the specified bounds.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from 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)

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.

(Inherited from Object)

Applies to

Product Versions
.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

See also