Xceed Grid for WinForms v4.3 Documentation
Using the Paint event

Welcome to Xceed Grid for WinForms v4.3 > Basic Concepts > Events > Using the Paint event

The Paint event is raised by each grid element after it has been painted in order to allow for additional custom painting of the grid element's foreground. To completely custom paint a grid element, jump to the Custom Painting topic.

Basic steps - C#

To subscribe to a Paint event, the following steps must be performed:

  • Obtain a reference to the desired grid element. 

  • Subscribe to the Paint event using the GridPaintEventHandler delegate class. 

  • Create a new method that will handle the events that are raised. 

  • Place the desired code in the newly created event handler.

Basic steps - VB.NET

To subscribe to a Paint event, the following steps must be performed:

Demonstration

This example demonstrates how to handle the Paint event for a group's margin.

VB.NET Copy Code

Dim group As New Group()

AddHandler Group.Margin.Paint, AddressOf Me.group_MarginPaint

Private Sub group_MarginPaint( ByVal sender As Object, ByVal e As GridPaintEventArgs )

  Dim margin As GroupMargin = CType( sender, GroupMargin )
  Dim brush As New SolidBrush( Color.Black )
  Dim starty As Integer = margin.ClientRectangle.Y

  Dim row As Row
  For Each row In margin.ParentGroup.HeaderRows  
    starty += row.Height
  Next row

  If starty < margin.ClientRectangle.Bottom Then
    Dim font As New Font( margin.Font.FontFamily, 8, FontStyle.Bold )
    Dim format As New StringFormat( StringFormatFlags.DirectionVertical )

    e.Graphics.DrawString( margin.ParentGroup.Title, font, brush, margin.ClientRectangle.X,starty, format )
    format.Dispose()
    font.Dispose()
  End If

  brush.Dispose()
End Sub

C# Copy Code

Group group = new Group();
group.Margin.Paint += new GridPaintEventHandler( this.group_MarginPaint );

private void group_MarginPaint( object sender, GridPaintEventArgs e )
{
  GroupMargin margin = ( GroupMargin )sender;

  using( SolidBrush brush = new SolidBrush( Color.Black ) )
  {
    int starty = margin.ClientRectangle.Y;
    foreach( Row row in margin.ParentGroup.HeaderRows )
      starty += row.Height;

    if( starty < margin.ClientRectangle.Bottom )
    {     
      using( Font font = new Font( margin.Font.FontFamily, 8, FontStyle.Bold ) )
      {
        using( StringFormat format = new StringFormat( StringFormatFlags.DirectionVertical ) )
        {
          e.Graphics.DrawString( margin.ParentGroup.Title, font, brush, 
                                 margin.ClientRectangle.X, starty, format );
        }
      }
    }
  }
}