Example E1290
Visible to All Users

Reporting for WinForms - How to Group Report Data by Days of the Week

The report in this example groups orders by day of the week taken from the order date. Orders are grouped by a calculated field that contains the day of the week for the Order data record.

Orders grouped by day of the week

Files to Review

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

GroupByTimeSpan/Form1.cs(vb)
C#
#region usings using System; using System.Drawing; using System.Windows.Forms; using DevExpress.DataAccess.ConnectionParameters; using DevExpress.DataAccess.Sql; using DevExpress.XtraReports.UI; #endregion namespace GroupByTimeSpan { public partial class Form1: Form { public Form1() { InitializeComponent(); } #region createreport 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; } #endregion private void button1_Click(object sender, EventArgs e) { // Create a report grouped by days of week, // and show its print preview. XtraReport report = CreateDataGroupingReport(); report.SaveLayoutToXml("test.repx"); report.ShowPreview(); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.