DataGridViewRowPrePaintEventArgs DataGridViewRowPrePaintEventArgs DataGridViewRowPrePaintEventArgs DataGridViewRowPrePaintEventArgs Class

Definition

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
Inheritance
DataGridViewRowPrePaintEventArgsDataGridViewRowPrePaintEventArgsDataGridViewRowPrePaintEventArgsDataGridViewRowPrePaintEventArgs

Examples

The following code example demonstrates how to handle the RowPrePaint event to draw a custom background for selected cells. 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 'dataGridView1_RowPrePaint

Remarks

The RowPrePaint event occurs before a row is painted on a DataGridView control. 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. Use the properties in DataGridViewRowPrePaintEventArgs to get the settings of the row without directly accessing the row in the DataGridView.

Constructors

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

Initializes a new instance of the DataGridViewRowPrePaintEventArgs class.

Properties

ClipBounds ClipBounds ClipBounds ClipBounds

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

ErrorText ErrorText ErrorText ErrorText

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

Graphics Graphics Graphics Graphics

Gets the Graphics used to paint the current DataGridViewRow.

Handled Handled Handled 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.

(Inherited from HandledEventArgs)
InheritedRowStyle InheritedRowStyle InheritedRowStyle InheritedRowStyle

Gets the cell style applied to the row.

IsFirstDisplayedRow IsFirstDisplayedRow IsFirstDisplayedRow IsFirstDisplayedRow

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

IsLastVisibleRow IsLastVisibleRow IsLastVisibleRow IsLastVisibleRow

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

PaintParts PaintParts PaintParts PaintParts

The cell parts that are to be painted.

RowBounds RowBounds RowBounds RowBounds

Get the bounds of the current DataGridViewRow.

RowIndex RowIndex RowIndex RowIndex

Gets the index of the current DataGridViewRow.

State State State State

Gets the state of the current DataGridViewRow.

Methods

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

Gets the Type of the current instance.

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

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)

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)

Applies to

See also