How to: Group Date-Time Values

Assume that the PivotGridControl is bound to a data table that contains an OrderDate field. In the following example two PivotGridField fields are created and displayed in the Column Header Area. Each is bound to the OrderDate field. The first Pivot Grid Control field’s values will be grouped by years, the second field’s values will be grouped by months. To group data, the PivotGridFieldBase.GroupInterval property is used.

The result of the code is shown below:


using DevExpress.XtraPivotGrid;

// Create column fields and bind them to the 'OrderDate' datasource field.
PivotGridField fieldYear = new PivotGridField("OrderDate", PivotArea.ColumnArea); 
PivotGridField fieldMonth = new PivotGridField("OrderDate", PivotArea.ColumnArea);
// Add the fields to the field collection.
// Set the caption and group mode of the fields.
fieldYear.GroupInterval = PivotGroupInterval.DateYear;
fieldYear.Caption = "Year";
fieldMonth.GroupInterval = PivotGroupInterval.DateMonth;
fieldMonth.Caption = "Month";