How to set the page breaks in print preview in WinForms GridControl?
Page breaks in print preview
To apply page breaks based on the bounds of the PrintPreview page in the WinForms GridControl, the page breaks should be calculated manually and added to the m_awPageFirstRow collection in the PrintPage event.
//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);
}
}'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 SubSamples:
Reference Link: 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 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!