Using VSView Reporting Edition > Add Grouping and Sorting Using Code |
Useful reports don't simply show data, they show it in an organized manner. VSReport8 uses groups to group and sort data. To demonstrate how this works, let's go back to our example and show the employees grouped by country.
The following code creates a group object that sorts and groups records by country. (It should be added to the previous code before the last block.)
Example Title |
Copy Code
|
---|---|
' group employees by country, in ascending order Dim grp As Group Set grp = vsr.Groups.Add("GrpCountry", "Country", vsrAscending) |
Every group has a header and a footer section. These are invisible by default, but we want to make the header section visible and show which country defines the group. The code is similar to what we used before to create the other sections, but this time we'll use a field with a solid background to make things a little different:
Example Title |
Copy Code
|
---|---|
With grp.SectionHeader .Height = 500 .Visible = True Set f = .Fields.Add("CtlCountry", "Country", _ 0, 0, vsr.Layout.Width, 500) f.Calculated = True f.Align = vsrLeftMiddle f.FontBold = True f.FontSize = 12 f.BorderStyle = vsrBSSolid f.BorderColor = RGB(0, 0, 150) f.BackStyle = vsrOpaque f.BackColor = RGB(150, 150, 220) f.MarginLeft = 100 End With |
To finish the new report, let's add another group to sort the employees within each country by their first name. Since we are only using this group to sort the names, we will leave the header and footer sections invisible.
Example Title |
Copy Code
|
---|---|
' sort employees by first name within each country vsr.Groups.Add "GrpName", "FirstName", vsrAscending |
We are done with our changes. To render the new report, we need to finish the routine with a call to the Render method, as before:
Example Title |
Copy Code
|
---|---|
' render the report into the VSPrinter control
vsr.Render vp
End Sub
|
Here is what the new report looks like: