Sorting by Summary
- 3 minutes to read
PivotGridControl can sort its column field values by any row and row field values by any column (the Sorting by Summary feature). The following picture shows a pivot grid with Product Name field values sorted by the 1995 column.
#End-User Sorting by Summary
For end-users, sorting by summary is available via context menus, invoked when right-clicking an innermost column or row header.
When sorting by summary is applied to a field, a column/row by which field values are sorted displays the or
image (sorting indicator) in its header.
End-users can toggle the sort order in one of the following ways.
- Click the header of the field whose values are sorted.
- Click the sorting indicator displayed in the header of the column/row by which field values are sorted.
#Availability
By default, sorting by summary is available for all fields if they are displayed within the Column Header Area or Row Header Area.
You can disable this functionality for end-users in two ways.
- For all fields - via the PivotGridControl.AllowSortBySummary property.
- For individual fields - via the PivotGridField.AllowSortBySummary property.
Note that in this instance, settings of individual fields take priority over PivotGridControl settings.
#Sorting by Summary in Code
To sort data by summaries in code, you need to specify a data field whose summary values should define the sort order.
To specify a data field, do one of the following.
- Assign the data field name to the PivotGridField.SortByFieldName property. In this instance, data will be sorted by summary values calculated with the function, whose type is specified by the PivotGridField.SortBySummaryType property, regardless of which summary values are currently displayed.
- Assign the data field instance to the PivotGridField.SortByField property. In this instance, data will be sorted by currently displayed summary values.
After you have specified a data field, PivotGridControl sorts field values by a Grand Total column/row that corresponds to this data field. To sort field values by any other column/row, you should identify this column/row via the PivotGridField.SortByConditions property.
The PivotGridField.SortByConditions property provides access to a collection of sort conditions (SortByCondition objects). Each condition identifies a field value, so that the whole collection identifies a column/row. For instance, the highlighted column on the image below is identified by three sort conditions.
To implement a sorting shown on this image, you should create three SortByCondition instances and add them to the PivotGridField.SortByConditions collection.
fieldTrademark.SortByField = fieldPrice;
fieldTrademark.SortByConditions.Add(new SortByCondition(fieldYear, 2002));
fieldTrademark.SortByConditions.Add(new SortByCondition(fieldMonth, 2));
fieldTrademark.SortByConditions.Add(new SortByCondition(fieldDay, 2));
To sort field values by a total column/row (for instance, the February Total column from the image above), you need to create only two sort conditions.
fieldTrademark.SortByField = fieldPrice;
fieldTrademark.SortByConditions.Add(new SortByCondition(fieldYear, 2002));
fieldTrademark.SortByConditions.Add(new SortByCondition(fieldMonth, 2));
To sort data by a custom total column/row, you should specify its type via the PivotGridField.SortByCustomTotalSummaryType property.
NOTE
In an OLAP mode, create sort conditions via a Sort
For an example, see How to: Sort Data by Individual Columns (Rows) in OLAP Mode.