wpf-6143-controls-and-libraries-data-grid-sorting-sorting-in-code.md
Note
Sorting by display text and custom sorting are not supported in Server Mode.
The following methods and properties can be used to sort data:
A column’s ColumnBase.SortOrder property specifies the column’s sort order. Set it to ColumnSortOrder.Ascending or ColumnSortOrder.Descending.
Set a column’s ColumnBase.SortIndex property to a non-negative value. This automatically sorts its values in ascending order.
Specify the DataControlBase.DefaultSorting property. The GridControl sorts its data by the values of the specified column in ascending order. Use the ColumnBase.SortOrder property to specify the sort order:
Use the grid’s GridControl.SortBy method.
Create a new instance of the GridSortInfo class, specify its GridSortInfo.FieldName and GridSortInfo.SortOrder properties, and add it to the grid’s GridControl.SortInfo collection.
To unsort data by the values of a single column, do one of the following:
Set the column’s ColumnBase.SortOrder property to ColumnSortOrder.None.
Set the column’s ColumnBase.SortIndex property to -1.
Use the grid’s GridControl.SortBy method:
Remove the corresponding GridSortInfo object from the GridControl.SortInfo collection.
To clear the sorting applied to the grid, use the DataControlBase.ClearSorting method.
Note
If grouping is applied, the DataControlBase.ClearSorting method preserves sorting applied to grouping columns, because group rows are always sorted.
When sorting data, the grid does the following:
Note
Group rows are always sorted. The StartSorting and EndSorting events are also raised before and after data is grouped.