Skip to main content

Group and Sort the Report Data

  • 4 minutes to read

This document describes a general technique to group and sort data in the report. For special cases, refer to the other help topics in this section.

Note that you can specify group and sort settings only for the report bound to the data source.

Use the Group and Sort Panel

Start with a new blank report and bind it to the Products table of the sample Northwind database:

group-and-sort-report-smart-tag-data-bound

You should specify both DataSource and DataMember property values.

Do the following to group and sort the report data:

  1. Click Group and Sort in the Visual Studio XtraReports menu to invoke the Group and Sort panel.

  2. Click Add a Group and select the data field by which you wish to group the data:

    group-data-panel-select-group-field

    This action creates an empty group header, a GroupField that relates to the selected data field, and adds the group field to the GroupHeaderBand.GroupFields collection.

    group-data-panel-field-selected

  3. Open the group header’s smart tag and click the ellipsis next to the GroupFields property editor to invoke the Group Field Collection Editor:

    group-data-band-field-collection

  4. You can use the Group Field Collection Editor to group data by multiple criteria. Click Add to create a new group field and specify its GroupField.FieldName property.

    group-data-field-collection-editor

    Use the arrow buttons to change the order in which these criteria are applied to the report data.

  5. In the Group and Sort panel, you can specify how the groups are sorted (in ascending or descending order). Select None if your groups are already ordered in the data source, and you do not need to sort them in the report.

    group-data-panel-sort-order

  6. Select Show Footer to create an empty GroupFooterBand for this group.

    group-data-panel-show-footer

  7. When a report has multiple groups, you can click Move Up or Move Down to change group order:

    group-data-panel-move-up

  8. Drag fields from the Field List and drop them onto the group header and detail bands to display field values in the report:

    group-data-drag-field-list

The resulting report looks as follows:

group-data-preview-result

Group Fields vs Group Headers

You can add multiple group fields to a single header or create multiple group headers. The report layout is different in these situations, as shown in the following image:

Single Group Header, multiple Group Fields Muliple Group Headers, each with a single Group Field
DataFeatures - Grouping6 DataFeatures - Grouping7

Specify Group Settings

You can use the smart tag to customize the group layout settings:

GroupUnion property
Allows you to keep content on the same page.
group-data-union-smart-tag
KeepTogether property
Allows you to print the group header/footer on the same page as the group content. group-data-keep-together-smart-tag
RepeatEveryPage property
Prints the group band on every page.
group-data-repeat-every-page-smart-tag
Band.PageBreak property
Starts a new page before or after a group.
group-data-page-break-smart-tag

Page Numbers in Groups

To display page numbers for individual groups, add the XRPageInfo control to the group header or footer and set its XRPageInfo.RunningBand property to the group header’s name:

HowTo_PageNumbers4Groups_4

Different groups should not appear on the same page for accurate page numbering. To meet this requirement, set the PageBreak property to AfterBand, or place the XRPageBreak control at the bottom of the band.

For more information, review the following help topic: Add Page Numbers.

Interactive Sort in Print Preview

You can allow end users to sort report data in Print Preview interactively. The user can click a designated element to change the sort order.

interactive-sorting-detail-result

For more information, review the following help topic: Sort a Report in Print Preview.

Group Band Without Group Fields

You can add a group band to a report without adding group fields to the band’s collection. The RepeatEveryPage property specifies how the report displays the group band without group fields:

RepeatEveryPage

Group Band Visibility

false

The band is shown once at the report’s beginning/end.

true

The band is shown on each page at the beginning/end.

See Also