Home › Forums › WinForms controls › Xceed Grid for WinForms › How to print page numbers
-
AuthorPosts
-
#17819 |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).
The following example demonstrates the minimum implementation required in order to print page numbers when printing the grid.
VB.NET
Imports Xceed.Grid
Public Class CustomGridPrintDocument
Inherits GridPrintDocumentPrivate m_grid As GridControl
Public Sub New(ByVal grid As GridControl)
MyBase.New(grid)
m_grid = grid
End SubProtected 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 ClassC#
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
Dim printDocument As New CustomGridPrintDocument(GridControl1)
printDocument.Print() C#
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.
Imported from legacy forums. Posted by Xceed admin (had 1106 views)
-
AuthorPosts
- You must be logged in to reply to this topic.