Skip to main content

How to: Simultaneously Sort and Group Data Against Specific Columns

The following example shows how to apply sorting and grouping to columns via the ColumnView.SortInfo collection. The columns which will be used to group and sort data are added using the GridColumnSortInfoCollection.ClearAndAddRange method. This method first clears the collection. The method’s integer parameter determines the number of the specified array’s elements that will be used to group data. In this example this parameter is set to 2 and thus the two first elements (CategoryID and SupplierID) will be used to group data. The third element (ProductName) refers to the column against which only sorting is applied.

using DevExpress.Data;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
// ...
GridView view = gridControl1.MainView as GridView;
GridColumnSortInfo [] sortInfo = { 
        new GridColumnSortInfo(view.Columns["CategoryID"], ColumnSortOrder.Ascending), 
        new GridColumnSortInfo(view.Columns["SupplierID"], ColumnSortOrder.Descending),
        new GridColumnSortInfo(view.Columns["ProductName"], ColumnSortOrder.Ascending)
                                 };
view.SortInfo.ClearAndAddRange(sortInfo, 2);