Group Data by a Custom Field
- 2 minutes to read
This tutorial illustrates how to group a report against a custom criteria at design time within Visual Studio. In the report created in this example, products are grouped by a calculated field that returns the first letter of the product name.
Do the following to group a report by a custom field:
- To create a table report in this tutorial, start with a report that is bound to the "Products" table of the sample Northwind database (the nwind.mdb file included in the XtraReports installation). To learn more about binding a report to a data source, see Provide Data to Reports.
As a result, the CalculatedField.DataMember property is automatically set to the "Products" table of the project's data set, which means that the calculated field can access data fields from this table.
Next, select the created field, set its CalculatedField.Name property to Initial (the field's CalculatedField.DisplayName will be automatically set to the same value) and set the CalculatedField.FieldType property to String.
Then click the ellipsis button for the calculated field's CalculatedField.Expression property, to invoke the Expression Editor.
Define the following expression in the editor that is invoked: Substring([ProductName], 0 ,1 ). This will extract the initial letter from the ProductName data field.
To close the editor and save the changes, click OK.
If the panel is not displayed, you can make it visible by checking the corresponding item in the XtraReports Menu.
To create a grouping criteria, click Add a Group and select the calculated field from the invoked drop-down menu.
You can also use this panel to specify whether or not the corresponding Group Footer band should be visible. The Sort Order drop-down list allows you to define the sort order within the group (ascending or descending) or disable sorting in grouped data.
Next, switch to the Field List and drop the required data fields onto the report's Detail band, and the Initial calculated field onto the Group Header area.