Xceed Grid for WinForms v4.3 Documentation
How to print page numbers

Welcome to Xceed Grid for WinForms v4.3 > Task-Based Help > Printing > How to print page numbers

By default, the grid is not printed with page numbers. In order to include page numbers when printing, you will need to create a class that derives from the GridPrintDocument class and override the OnPrintPage method (to be able to custom print the header or footer in this case, or other items if desired). 

Demonstration

The following example demonstrates the minimum implementation required in order to print page numbers when printing the grid.

VB.NET Copy Code

Imports Xceed.Grid 

Public Class CustomGridPrintDocument
  Inherits GridPrintDocument 

Private m_grid As GridControl 

Public Sub New(ByVal grid As GridControl)
  MyBase.New(grid)
  m_grid = grid
End Sub 

Protected Overrides Sub OnPrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
      Dim gc As GraphicsContainer = e.Graphics.BeginContainer() 

      Try
         MyBase.OnPrintPage( e )
      Finally
         e.Graphics.EndContainer(gc)
      End Try 

      ' Print added text boxes (header and footers)
      If Not e.Cancel And Me.IsCurrentPrintingPageSelected Then
        Dim brush As New System.Drawing.SolidBrush(Color.Black)

        e.Graphics.DrawString("Page number " + _
                              Me.CurrentPageNumber.ToString(), m_grid.Font, brush, _
                              New RectangleF(e.MarginBounds.X, e.MarginBounds.Y - 20, _
                              e.MarginBounds.Width, e.MarginBounds.Height))

        brush.Dispose()
      End If
End Sub

End Class

C# Copy Code

using Xceed.Grid;

public class CustomGridPrintDocument : GridPrintDocument
{
  private GridControl m_grid;

  public CustomGridPrintDocument( GridControl grid )
    :base( grid )
  {
    m_grid = grid;
  }

  protected override void OnPrintPage( System.Drawing.Printing.PrintPageEventArgs e )
  {
    GraphicsContainer gc = e.Graphics.BeginContainer();     

    try
    {
      base.OnPrintPage( e );
    }
    finally     
    {        
      e.Graphics.EndContainer( gc );     
    }      

    // Print page number     
    if( !e.Cancel )
    {
      using( System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush( Color.Black ) )
      {
        e.Graphics.DrawString("Page number " + this.CurrentPageNumber.ToString(),
                              m_grid.Font, brush, new RectangleF( e.MarginBounds.X,
                              e.MarginBounds.Y - 20,
                              e.MarginBounds.Width, e.MarginBounds.Height ) );
      }
    }
  }
}

To use the custom GridPrintDocument, the following code must be used:
VB.NET Copy Code

Dim printDocument As New CustomGridPrintDocument(GridControl1)
printDocument.Print()

C# Copy Code

CustomGridPrintDocument printDocument = new CustomGridPrintDocument( gridControl1 );
printDocument.Print();

Keep in mind that if you call the grid's Print or PrintPreview methods, the grid will always be printed with the default print document.