windowsforms-114632-controls-and-libraries-data-grid-getting-started-walkthroughs-summaries-tutorial-sort-group-rows-by-summary-values.md
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
Start with a GridControl already grouped against the Category column.
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.
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})"));
}
Imports DevExpress.Data
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Columns
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
gridView1.GroupSummary.Clear()
Dim summaryItemMaxOrderSum As GridSummaryItem = gridView.GroupSummary.Add(New GridSummaryItem(DevExpress.Data.SummaryItemType.Max, "OrderSum", Nothing, "(MAX Order Sum = {0:c2})"))
End Sub
Run the application. Group rows display the maximum values of the OrderSum column within each group.
Right-click the Category column header, select the Sort by Summary item, and then Sort Ascending.
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.
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.
Select the grid View, expand its GridView.OptionsMenu property and disable the GridOptionsMenu.ShowGroupSortSummaryItems option.
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.
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);
}
Imports DevExpress.Data
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Columns
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' ...
Dim firstGroupColumn As GridColumn = gridView1.SortInfo(0).Column
Dim groupSummaryToSort() As GroupSummarySortInfo = { New GroupSummarySortInfo(summaryItemMaxOrderSum, firstGroupColumn, ColumnSortOrder.Ascending) }
gridView1.GroupSummarySortInfo.ClearAndAddRange(groupSummaryToSort)
End Sub
Run the application to see the result. Group rows will now be sorted by summary values in ascending order.
See Also
Tutorial: Obtain Summary Values