Skip to main content

Group Data by Days of the Week (Runtime Sample)

  • 3 minutes to read

This topic contains a code sample that creates the Group Header band in code to group report data by days of the week. The report in this example groups orders by day of the week calculated for the order date, as the following image illustrates:

Group Data by Days of the Week

The report created in this example uses the Northwind SQLite database. The nwind.db database file ships with the DevExpress installation (the default path is C:\Users\Public\Public Documents\DevExpress Demos 23.2\Components\Data\nwind.db).

This report contains two bands: the Detail band and the Group Header band. The Group Header band includes a group field that specifies that the data is grouped by the calculated field.

using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
using DevExpress.XtraReports.UI;
// ...
public XtraReport CreateDataGroupingReport() {
    // Create a report, and bind it to a data source.
    XtraReport report = new XtraReport();

    SqlDataSource sqlDataSource = new SqlDataSource(
        new SQLiteConnectionParameters() {
            FileName = "nwind.db",
            Password = null
        });
    SelectQuery queryOrders = SelectQueryFluentBuilder
        .AddTable("Orders")
        .SelectAllColumnsFromTable()
        .Filter("[OrderDate] > #2016-01-01#")
        .Build("Orders");
    sqlDataSource.Queries.Add(queryOrders);
    sqlDataSource.Fill();

    report.DataSource = sqlDataSource;
    report.DataMember = "Orders";

    // Create a Detail band, and add it to the report.
    DetailBand detailBand = new DetailBand();
    detailBand.Height = 20;
    report.Bands.Add(detailBand);

    // Create a Group Header band, and add it to the report.
    GroupHeaderBand ghBand = new GroupHeaderBand();
    ghBand.Height = 20;
    report.Bands.Add(ghBand);

    // Create a calculated field, and add it to the collection.
    CalculatedField calcField = new CalculatedField(report.DataSource, report.DataMember);
    calcField.Name = "calcDayOfWeek";
    calcField.FieldType = FieldType.None;
    calcField.Expression = "GetDayOfWeek([OrderDate])";
    report.CalculatedFields.Add(calcField);

    // Specify the calculated field as a grouping basis.
    GroupField groupField = new GroupField();
    groupField.FieldName = "calcDayOfWeek";
    ghBand.GroupFields.Add(groupField);

    // Create two data-bound labels and add them to the related report bands.
    XRLabel ghLabel = new XRLabel();
    ghLabel.DataBindings.Add("Text", report.DataSource, "Orders.OrderDate", "{0:dddd}");
    ghLabel.BackColor = Color.SteelBlue;
    ghLabel.ForeColor = Color.White;
    ghBand.Controls.Add(ghLabel);

    XRLabel detailLabel = new XRLabel();
    detailLabel.DataBindings.Add("Text", report.DataSource, "Orders.OrderDate", "{0:MM/dd/yyyy}");
    detailLabel.ProcessDuplicatesTarget = ProcessDuplicatesTarget.Value;
    detailLabel.ProcessDuplicatesMode = ProcessDuplicatesMode.Suppress;
    detailLabel.LeftF = 20;
    detailBand.Controls.Add(detailLabel);

    return report;
}

View Example: How to Group Report Data by Days of the Week