Export (0) Print
Expand All
Expand Minimize

DataGridView.RowPostPaint Event

Occurs after a DataGridViewRow is painted.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Event RowPostPaint As DataGridViewRowPostPaintEventHandler

You can handle this event alone or in combination with the RowPrePaint event to customize the appearance of rows in the control. You can paint entire rows yourself, or paint specific parts of rows and use the following methods of the DataGridViewRowPostPaintEventArgs class to paint other parts:

You can also use the VisualStyleRenderer class to paint standard controls using the current theme. For more information, see Rendering Controls with Visual Styles. If you are using Visual Studio 2005, you also have access to a large library of standard images that you can use with the DataGridView control.

For more information about handling events, see Consuming Events.

The following code example demonstrates how to use a handler for the RowPostPaint event to paint textual content that spans the entire row below the normal cell values. This example is part of a larger example available 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. 
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 'dataGridView1_RowPostPaint

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft