You often want to examine data grouped on the basis of one or more criteria. These criteria are usually expressions based on one or more table fields, but can be much more complex. You designate a data group in a report by associating a Group on expression, your criterion for grouping the records, with the report.
For each set of records in which the expression evaluates to one value, you can designate certain actions you want the Report Engine to perform. These actions include providing summary content in the report output, and triggering program code.
Using Multiple Data Groups
Data groups are nested, which can be useful for organizing data and totaling expressions at different levels. For example, you can group records in a report by country by creating data groups based on the country name field. You can then group records by region within each country as a nested group.
To determine the nesting level for a data group, consider how often the value of the expression for the data group might change, then define the group that changes most often first. In the example report described in this section, the value of a country field changes more often than the value of a region field. Therefore, the country should be the outer two group, and the region should be the inner group.
Putting Data in Order for Groups
To group records effectively, you must sort the records appropriately before running the report. If the records with the same group value don't appear consecutively in the output, the Report Engine can't display the summary information or calculate your group-related values.
The report layout does not actually sort and order your data. It processes records in the same order that they exist in the data source. Sorting and ordering must be performed with a view, index, or other form of data manipulation outside the layout. For example, if the data source is a table, its records are probably not in the appropriate order for grouping. You can sort and order the data by setting an appropriate index on the table, using an ordered view in the data environment, or using a query as the data source to display the records in groups.
In the sample report described above, which contains two group levels, the records in the data source must be sorted country name and also by state or region within country, to appear properly in the output. If the report's data source is a table, you index the table on a key expression such as Country + Region
.
Note: |
---|
Your index can contain additional elements not needed by the data groups in your report. In this example, your index expression could read Country + Region +City. This order would provide a sorted list of cities in the details you display for each region.
|
Report Display Options relevant to Data Groups
When you add data groups, you can preface and follow each group with a group header and group footer, which appear on the report layout as a Group Header band and a Group Footer band. Typically, the Group Header band contains a Field control for the field used by the group. You can also add other controls such as shapes or labels that display before the first record in a group. The Group Footer band often contains group totals and other summary information for the group.
You can also specify other options for data groups:
-
Printing text in headers and footers to identify specific groups.
-
Printing each group on a new page.
-
Resetting page numbers when groups are printed on a new page.
Triggering Code for a Data Group
You can perform actions programmatically for each data group. For example, each time a new region starts, you could insert a record into a cursor, with fields for the region name and the system variable _PAGENO. After running the report, you could use this cursor to provide a second report with an index or table of contents.
To perform this task during the run of the report, write a user-defined function and specify its name as the code to run during the On entry or On exit event for the Group Header band.
For more information, see How to: Specify Expressions to Evaluate When Processing Bands.