)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: December 19, 2019

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

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon