Today I want to tell you how to set page number for every group in MS Reporting services. The approach differs depend on what version of reporting services you use.
If you use MS Reporting services 2008 R2 then you can use first approach with ease. But if you use free MS Reporting services redistributable package use second approach.
If you use MS Reporting services in your project it’s not a problem to set new page number for every group.
You can take a look at this post how to achieve it
In short, just to set PageBreak -> ResetPageNumber property to true and this works in MS Reporting services 2008 R2.
But in my project I used MS Report viewer redistibutable 2008 and this version doesn’t have such property.
So to set up the page number for every group from the beginning I use the following algorithm.
1. Add the textbox to the page with the group field value. Simply to say, field by what you group your report.
Set the Name property for it, for example GroupValue. If you don’t want this field shold be visible just set it’s visibility to false in textbox properties.
2. Go to the report properties fild Code value and add some code.
For adding code to the page simply choose “Report” menu.
Then “Report Properties” and finally “Code” tab.
The idea of algorithm is to set the page number once, save it’s number to array and when we flip pages back, not to count it from beginning, but take page number from global pages array.
This idea works perfectly and here some code how to set this solution up.
Pages array which we create when the report loads, the array has the number of elements the same as total number of pages.
And if elements in pages array is less then total number of pages (we can take it from global variables)
we add the page number to array. So, when we’ll add all report pages to array, next time we can take the desired page number from this array.
It’ll be easier to figure it out when you take a look at this code:
Shared offset as Integer
Shared currentgroup as Object
Shared currentPage as Integer
Shared Dim pagesArray() As Integer
Public Function GetGroupPageNumber(group as Object, pagenumber as Integer, totalpages as Integer) as Object
If Not (group = currentgroup)
offset = pagenumber - 1
currentgroup = group
If (pagesArray Is Nothing) Then
ReDim Preserve pagesArray(totalpages)
If (CountNumberOfElementsInArray(pagesArray) < totalpages)
pagesArray(pagenumber - 1) = pagenumber - offset
Return pagesArray(pagenumber - 1)
Public Function CountNumberOfElementsInArray(array() As Integer) as Integer
Dim counter as Integer
Dim i As Integer
counter = 0
For i = 0 To array.Length - 1
If (array(i) <> 0)
counter = counter + 1
3. And the third step is to invoke the function from the report’s page.
Just add the textbox to the header of the page where you want to see page number and add next function call.
This calls the function written earlier.
Hope you’ll find something useful for you in my post!