Add Page Breaks Between Groups in RadGrid PDF Export
Environment
Product | RadGrid for ASP.NET AJAX |
Version | 2024.3.805 |
Description
When exporting a RadGrid to PDF, it's required to start each group on a new page. To achieve this, a page break needs to be inserted after each group's end. This KB article also answers the following questions:
- How can I ensure each group in RadGrid starts on a new page when exporting to PDF?
- Is it possible to insert page breaks in RadGrid's PDF export based on the group change?
- How to customize RadGrid's PDF export to have each group on separate pages?
Solution
To insert a page break on each group change when exporting RadGrid to PDF, follow these steps:
-
Enable the Group Footer in the
MasterTableView
by setting theShowGroupFooter
property totrue
.ASPX<MasterTableView ShowGroupFooter="true">
-
Inject the Page Break into the Footer of Each Group. In the
PreRender
event of RadGrid, iterate through the Group Footer items and add the page break into their cells.C#protected void RadGrid1_PreRender(object sender, EventArgs e) { // Get all GroupFooter items GridItem[] groupFooterItems = RadGrid1.MasterTableView.GetItems(GridItemType.GroupFooter); // Iterate through all except the last for (int i = 0; i < groupFooterItems.Length - 1; i++) { // Inject the page break into the first cell of the Group Footer groupFooterItems[i].Cells[0].Text = "<?hard-pagebreak ?>"; } }
-
Optional: Hide the Group Footer. If you do not wish to display the Group Footer in the exported PDF, use CSS to target the Footer element and hide it.
CSS<style> .RadGrid .rgMasterTable .rgFooter { display: none; } </style>
This setup ensures that each group in the RadGrid starts on a new page in the exported PDF by utilizing a hard page break at the end of each group.