On some reports, you may want to group sets of data together and reset the page number after each group. For example, in the standard application, report 206, Sales Invoice, displays sales invoices that are grouped by sales invoice number. To reset the page number to 1 for each group of data, you must modify code on the client report definition (RDLC) report layout.
Before you begin this procedure, you must create a layout suggestion for the report. For more information, see How to: Create a Layout Suggestion.
To reset page numbers
-
In the Classic client, on the Tools menu, click Object Designer.
-
In Object Designer, click Report, select a report that you want to modify, and then click Design.
-
On the View menu, click Layout.
-
In Microsoft Visual Studio, in the Report.rdlc file, on the Report menu, select Report Properties.
-
In the Report Properties window, select the Code tab.
-
Add the following code in the Custom code text box:
Visual Basic Copy Code REM Reset Page Number: Shared offset As Integer Shared newPage As Object Shared currentgroup1 As Object Shared currentgroup2 As Object Shared currentgroup3 As Object Public Function GetGroupPageNumber(ByVal NewPage As Boolean, ByVal pagenumber As Integer) As Object If NewPage Then offset = pagenumber - 1 End If Return pagenumber - offset End Function Public Function IsNewPage(ByVal group1 As Object, ByVal group2 As Object, ByVal group3 As Object) As Boolean newPage = False If Not (group1 = currentgroup1) Then newPage = True currentgroup1 = group1 Else If Not (group2 = currentgroup2) Then newPage = True currentgroup2 = group2 Else If Not (group3 = currentgroup3) Then newPage = True currentgroup3 = group3 End If End If End If Return newPage End Function
Note This code is also available in report 206, Sales Invoice.
-
Click OK.
-
In Visual Studio, in the Report.rdlc file, right-click the text box in the header that displays the page number, and then select Expression.
-
In the Expression window, enter the following expression.
Copy Code =Code.GetGroupPageNumber(ReportItems!NewPage.Value,Globals!PageNumber)
The NewPage.Value parameter is the value of the NewPage text box, which you create in step 8.
-
Click OK.
-
In the Body section of the layout, create a new text box called NewPage.
-
Right-click the NewPage text box that you created in step 8 and then select Properties.
-
In the Properties window, on the General tab, enter the following in the Value field.
Copy Code =Code.IsNewPage(Fields!<grouping field1>[,<grouping field2>,<grouping field3>])
For example, if you are grouping by document type, then grouping field1 would be Document_Type.Value.
Note If you want to use more than three groupings, you can modify the code in the IsNewPage function. If you want to use fewer than three groupings, you can either modify the code in the IsNewPage function or use a static value for one or more of the parameters.
-
Click OK.