Skip to main content

Tutorial: Sort Group Rows by Summary Values

  • 3 minutes to read

This walkthrough is a transcript of the Sort Group Rows by Summary Values video available on the DevExpress YouTube Channel.

The tutorial first shows the grid’s built-in UI allowing you to sort group rows by summary values. You will then see how to disable this UI if needed, and how you can sort group rows in code.

Launch demo: Sorting by Summary Values

Starting Point

Start with a GridControl already grouped against the Category column.

GridView_Summaries_InitialGridForSortBySummary

By default, group rows are automatically sorted in alphabetical order where the “Beverages” group row is followed by “Condiments”, then by “Confections” and so on.

Adding a Group Summary

In the form Load event handler, create a group summary that calculates the maximum value in the OrderSum column. The summary values will be displayed in group rows.

using DevExpress.Data;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;

private void Form1_Load(object sender, EventArgs e) {
    gridView.GroupSummary.Clear();
    GridSummaryItem summaryItemMaxOrderSum = gridView.GroupSummary.Add(new GridSummaryItem(DevExpress.Data.SummaryItemType.Max, "OrderSum", null, "(MAX Order Sum = {0:c2})"));
}

Run the application. Group rows display the maximum values of the OrderSum column within each group.

GridView_Summaries_OrderSumMaxForSorting

End-User Capabilities

Right-click the Category column header, select the Sort by Summary item, and then Sort Ascending.

GridView_Summaries_SortBySummaryMenuItem

As a result, group rows are now sorted in ascending order based on summary values. A special glyph displayed within the grouping column indicates the current sort order.

GridView_Summaries_SortBySummaryResult

In the same manner, you can reverse the sort order. To remove group row sorting, right-click the Category column header and select Clear Summary Sorting.

GridView_Summaries_ClearSummarySortingMenuItem

Restricting End-User Capabilities

Select the grid View, expand its GridView.OptionsMenu property and disable the GridOptionsMenu.ShowGroupSortSummaryItems option.

GridView_Summaries_ShowGroupSortSummaryItems

Run the application and right-click the Category column’s header. The context menu that appears now as a result doesn’t contain the Sort by Summary item.

Sorting by Summary Values in Code

Close the application again and return to the form Load event handler. Obtain the first grouping column from the View’s ColumnView.SortInfo collection. To sort group rows in code, create a new GroupSummarySortInfo object. Use the summary item, target column and required sort order as parameters. Finally, clear the GridView.GroupSummarySortInfo collection and add the created object to it using the GroupSummarySortInfoCollection.ClearAndAddRange method.

using DevExpress.Data;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;

private void Form1_Load(object sender, EventArgs e) {
    // ...
    GridColumn firstGroupColumn = gridView.SortInfo[0].Column;
    GroupSummarySortInfo[] groupSummaryToSort = { new GroupSummarySortInfo(summaryItemMaxOrderSum, firstGroupColumn, ColumnSortOrder.Ascending) };
    gridView.GroupSummarySortInfo.ClearAndAddRange(groupSummaryToSort);
}

Run the application to see the result. Group rows will now be sorted by summary values in ascending order.

See Also