How to print only the specific pages of the grid in WinForms GridControl?
Printing
To customize the specific pages for printing in the WinForms GridControl, you can use the PrintPage event. This event can be triggered for each page during printing. You can also use the BeginPrint event. This event is triggered once printing is performed.
The default value of the PrintRange is All. For printing the specific pages, it should be changed to PrintRange.SomePages through the BeginPrint event.
In the following code example, the first and second pages are selected for printing.
Syncfusion.GridHelperClasses.GridPrintDocumentAdv pd = new Syncfusion.GridHelperClasses.GridPrintDocumentAdv(this.gridControl1);
//Triggers the BeginPrint event
pd.BeginPrint += new System.Drawing.Printing.PrintEventHandler(pd_BeginPrint);
//Triggers the PrintPage event
pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage);
//Handles the BeginPrint event
private void pd_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
((GridPrintDocumentAdv)sender).PrinterSettings.PrintRange = PrintRange.SomePages;
//Prints 1st and 2nd page
((GridPrintDocumentAdv)sender).PrinterSettings.FromPage = 1;
((GridPrintDocumentAdv)sender).PrinterSettings.ToPage = 2;
}
//Handles the PrintPage event
private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Customizes your code here
Console.WriteLine("The page is printing...");
}Private pd As New Syncfusion.GridHelperClasses.GridPrintDocumentAdv(Me.gridControl1)
'Triggers the BeginPrint event
Private pd.BeginPrint += New System.Drawing.Printing.PrintEventHandler(AddressOf pd_BeginPrint)
'Triggers the PrintPage event
Private pd.PrintPage += New System.Drawing.Printing.PrintPageEventHandler(AddressOf pd_PrintPage)
'Handles the BeginPrint event
Private Sub pd_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs)
CType(sender, GridPrintDocumentAdv).PrinterSettings.PrintRange = PrintRange.SomePages
'Prints 1st and 2nd page
CType(sender, GridPrintDocumentAdv).PrinterSettings.FromPage = 1
CType(sender, GridPrintDocumentAdv).PrinterSettings.ToPage = 2
End Sub
'Handles the PrintPage event
Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
'Customizes your code here
Console.WriteLine("The page is printing...")
End SubNote:
In the above example, particular pages are printed when you press the print button. The Range of pages can be given by using the FromPage and ToPage properties of the PrinterSettings.
Samples:
C#: Somepages print-C#
VB: Somepages print-VB
Reference Link: Printing
Conclusion
I hope you enjoyed learning about how to print only the specific pages of the grid in WinForms Grid Control.
You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations and WinForms GridControl documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!