How to set the page breaks in print preview based on the page size in WinForms GridControl?
Page breaks in print preview
To apply page break based on the bounds of PrintPreview page in WinForms GridControl, the page breaks should be calculated manually and added to the m_awPageFirstRow collection in the PrintPage event.
Code Snippet
C#
//Event Subscription gridPrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(GridPrintDocument_PrintPage); //Event Customization private void GridPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { pageIndex += 1; this.gridControl1.PrintInfo.m_awPageFirstRow = new ArrayList(); if (pageIndex <= pageBreaks.Count - 1) { int height = this.gridControl1.Model.RowHeights.GetTotal(pageBreaks[pageIndex - 1], pageBreaks[pageIndex]); if (e.MarginBounds.Height < height) { //To avoid last getting cut off. int rows = (e.MarginBounds.Height / this.gridControl1.DefaultRowHeight) - 2; int insertRow = pageBreaks[pageIndex - 1]; int temp = pageIndex; while (pageBreaks[temp] > (insertRow + rows)) { insertRow += rows; pageBreaks.Insert(temp, insertRow); temp += 1; } } } foreach ( int row in pageBreaks) { this.gridControl1.PrintInfo.m_awPageFirstRow.Add(row); } }
VB
'Event Subscription AddHandler gridPrintDocument.PrintPage, AddressOf GridPrintDocument_PrintPage 'Event Customization Private Sub GridPrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) pageIndex += 1 Me.gridControl1.PrintInfo.m_awPageFirstRow = New ArrayList() If pageIndex <= pageBreaks.Count - 1 Then Dim height As Integer = Me.gridControl1.Model.RowHeights.GetTotal(pageBreaks(pageIndex - 1), pageBreaks(pageIndex)) If e.MarginBounds.Height < height Then 'To avoid last getting cut off. Dim rows As Integer = (e.MarginBounds.Height / Me.gridControl1.DefaultRowHeight) - 2 Dim insertRow As Integer = pageBreaks(pageIndex - 1) Dim temp As Integer = pageIndex Do While pageBreaks(temp) > (insertRow + rows) insertRow += rows pageBreaks.Insert(temp, insertRow) temp += 1 Loop End If End If For Each row In pageBreaks Me.gridControl1.PrintInfo.m_awPageFirstRow.Add(row) Next row End Sub
Samples:
Reference link: https://help.syncfusion.com/windowsforms/grid-control/printing
Conclusion
I hope you enjoyed learning about how to set the page breaks in print preview based on the page size in WinForms GridControl.
You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms 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 WinForms GridControl and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!