How to Customize Page Numbering Options in a .NET Report
We often get asked how to add custom page numbers to a report or add page numbers based on a grouping to a report. In this blog, we will discuss these two frequently asked questions in ActiveReports:
- Display Page Numbers in ActiveReports.NET on the based on Groups present in the report
- Display a custom message on the Last Page of each group.
Let's create a report listing the available and discontinued products based on Northwind's Products table. The report will be grouped by the Discontinued field of the table, spanning different numbers of pages.
Once we group our data, assuming that Group1 will fit on one page and Group2 will span three pages, the numbering for the first page of the Report would be 1 of 1. The counter would be reset, and now the numbering for the second Group would be printed like 1 of 3, 2 of 3, 3 of 3.
We approach both tasks depending on which report type we use. For Section Reports, we will use scripts. While in Page and RDL reports, we will use properties and expressions.
Section Reports
Display Page Numbers on the Basis of Groups
The solution is simple. Use ReportInfo Control to show page numbering based on groups, and set its properties as follows:
- Format: Page {PageNumber} of {PageCount}
- SummaryGroup: GroupHeader1
- SummaryRunning: Group
That's it, and you are good to go!
Display Custom Message on Last Page
You may want to display page numbers for each group while showing a Custom Message on the last page of the group. In this case, you need to perform one extra step. Once the ReportInfo Control has been added and its properties set as mentioned above, drag and drop a TextBox Control from the toolbox.
Place this control exactly over the ReportInfo Control and set its Visible property to False. We will use the TextBox Control to show a custom message. Toggle the visibility of these two controls, ReportInfo and TextBox, in the Format event of GroupHeader and GroupFooter to get the desired results. Use a script like the one below:
Sub GroupFooter1_Format
ReportInfo1.Visible = false
TextBox3.Visible = True
TextBox3.text = "This is the last page of this group."
End Sub
Sub GroupHeader1_Format
ReportInfo1.Visible = true
TextBox3.Visible = false
End Sub
This is the result we get:
Page and RDL Reports
Display Page Numbers on the Basis of Groups
This solution is even simpler. Use the predefined Page N of M (Section) expression from the Common Values found in the Report Explorer panel to show page numbering-based groups. Just remember that when you create your grouping in your report, you check the Has own page numbering setting for the group's layout:
Display Custom Message on Last Page
To display a Custom Message on the last page of the group while keeping the page numbering as discussed above, you can use an Expression. Select the Page N of M (Section) textbox and change its expression value to:
=IIF(Globals!PageNumberInSection = Globals!TotalPagesInSection, "Page " & Globals!PageNumberInSection & " of " & Globals!TotalPagesInSection & ". This is the last page of this group. ", "Page " & Globals!PageNumberInSection & " of " & Globals!TotalPagesInSection)
This expression states that if the current page is the last, append 'This is the last page of this group.' to the page numbering. Otherwise, leave it as Page N of M. With that, this is the result we get:
Conclusion
To see the complete implementation, download the zip file here: CustomPageNumbering.zip
Learn more about ActiveReports.NET features by visiting our Online Demos.