Group Data by Days of the Week
- 3 minutes to read
This tutorial shows you how to group report data by days of the week in the Visual Studio Report Designer. You can use the method described in this topic to group data by month, or by any other time span value.
#Prerequisites
Do the following to create a blank report bound to the Orders data table:
- Add a new blank report to the project. Review the following help topic for details: Create a Report in Visual Studio.
Click Add Report Data Source in the report’s smart tag menu to invoke the Data Source Wizard, as illustrated in the following image:
- Create a data source bound to the Orders table in the sample Northwind database. Review the following help topic for more information: Data Source Wizard.
Click Field List in the Visual Studio XtraReports menu to invoke the Field List panel. The panel appears as follows:
#Create a Calculated Field
In the Field List panel, right-click any data field and select Add Calculated Field in the context menu. Review the following help topic for more information: Calculated Fields.
The DataMember property for the created field is set to the Orders table and the calculated field can access data fields from this table.
Right-click the created field and select Edit Calculated Fields:
In the invoked Calculated Field Collection Editor, specify the Name property. The DisplayName property is automatically set to the same value.
Click the ellipsis button next to the Expression property to invoke the Expression Editor. Enter the following expression in the editor:
GetDayOfWeek([OrderDate])
The expression returns a zero-based index of the day of the week from the OrderDate data field. For more information on expressions, review the following help section: Use Expressions.
To close the editor and save the changes, click OK.
To close the editor and save the changes, click OK.
#Group by the Calculated Field
Use the Group and Sort panel to create a Group Header band associated with the calculated field. If the panel is hidden, click Group and Sort in the XtraReports menu:
To create grouping criteria, click Add a Group and select the calculated field from the invoked drop-down menu:
You can use this panel to show or hide the related Group Footer band. For more information, review the following help topic: Group and Sort Panel.
Click Add a Sort and select the OrderID field to sort the displayed data by that field:
Switch to the Field List and drop the OrderDate data field onto the report’s Group Header band. Drop the OrderID, OrderDate, and CustomerID fields onto the Detail band:
In the smart tag menu, click the Format String item’s ellipsis button to change the format string of the label in the Group Header band bound to the OrdersDate field:
Set the format string to the {0:dddd} value. This value instructs the label to display the day of the week instead of the date:
Switch to the Preview tab to see the result: