Articles in this section
Category / Section

How to print the header and footer in a grid with page count in WinForms GridControl?

2 mins read

Print the header and footer

In GridControl, you can print the header and footer of grid with page count. To achieve this, you need to customize the Header and Footer texts by deriving the GridPrintDocument and by overriding its OnPrintPage method.

The following code example explains how to customize the header and footer texts.

C#

protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs ev)
{
   base.OnPrintPage(ev);
   //compute the position of the header
   float x = (float)this.DefaultPageSettings.Bounds.X;//this.DefaultPageSettings.Margins.Left;
   float y = (float)this.DefaultPageSettings.Margins.Top - headerHeight - 30;
   float w = (float)this.DefaultPageSettings.Bounds.Width;
   float h = (float)headerHeight + 30;
   //setup the alignment
   StringFormat format = new StringFormat();
   format.LineAlignment = StringAlignment.Center;
   format.Alignment = StringAlignment.Center;
   //define the header text and draw it
   string text = string.Format("Header Text {0}", pageNo);
   Font font = new Font(_grid.Font.Name, 30);
   ev.Graphics.DrawString(text, font, Brushes.Black, new RectangleF(x, y, w, h), format);
   //position the footer; align the footer ; print the footer
   y = (float)(this.DefaultPageSettings.Bounds.Top + this.DefaultPageSettings.Bounds.Height - footerHeight);// Margins.Top - headerHeight - 30;
   h = (float)footerHeight;
   format.Alignment = StringAlignment.Center;
   // Gets the page No. with the Maximum Page
   text = string.Format("Footer Text {0} of {1}", pageNo, this.PrinterSettings.MaximumPage);
   font = new Font(_grid.Font.Name, 10);
   ev.Graphics.DrawString(text, font, Brushes.Black, new RectangleF(x, y, w, h), format);
   pageNo++;
}

 

VB

Protected Overrides Sub OnPrintPage(ByVal ev As System.Drawing.Printing.PrintPageEventArgs)
            MyBase.OnPrintPage(ev)
   'compute the position of the header
   Dim x As Single = CSng(Me.DefaultPageSettings.Bounds.X) 'this.DefaultPageSettings.Margins.Left;
   Dim y As Single = CSng(Me.DefaultPageSettings.Margins.Top) - headerHeight - 30
   Dim w As Single = CSng(Me.DefaultPageSettings.Bounds.Width)
   Dim h As Single = CSng(headerHeight) + 30
   'setup the alignment
   Dim format As New StringFormat()
   format.LineAlignment = StringAlignment.Center
   format.Alignment = StringAlignment.Center
   'define the header text and draw it
   Dim text As String = String.Format("Header Text {0}", pageNo)
   Dim font As New Font(_grid.Font.Name, 30)
   ev.Graphics.DrawString(text, font, Brushes.Black, New RectangleF(x, y, w, h), format)
   'position the footer; align the footer ; print the footer
   y = CSng(Me.DefaultPageSettings.Bounds.Top +Me.DefaultPageSettings.Bounds.Height - footerHeight) 
   ' Margins.Top - headerHeight - 30;
   h = CSng(footerHeight)
   format.Alignment = StringAlignment.Center
   ' Gets the page No. with the Maximum Page
   text = String.Format("Footer Text {0} of {1}", pageNo,
   Me.PrinterSettings.MaximumPage)
   font = New Font(_grid.Font.Name, 10)
   ev.Graphics.DrawString(text, font, Brushes.Black, New RectangleF(x, y, w, h), format)
   pageNo += 1
End Sub

 

The following screenshot displays the GridControl print preview with header, footer and page count.

Display header and footer in printing page

Sample: http://www.syncfusion.com/downloads/support/directtrac/general/Wf-14486-How_print_headerAndFooter_PageCount-1379188120.zip

Reference link: https://help.syncfusion.com/windowsforms/grid-control/printing

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied