Log In
[Expand]General Information
[Collapse]WinForms Controls
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Collapse]Data Grid
   [Expand]Getting Started
   [Expand]Binding to Data
   [Expand]Grid View
   [Expand]View Technology
     Sorting Overview
     Sortable and Non-Sortable Columns
     Tutorial: Custom Sorting and Non-Sortable Columns
     Tutorial: Data Sorting Basics
     Tutorial: Sorting by Values or Display Text
   [Expand]Data Editing
   [Expand]Filtering and Locating Rows
   [Expand]Focus and Selection Handling
   [Expand]Processing Rows
   [Expand]Formatting Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
   [Expand]Batch Modifications
   [Expand]Hit Information
   [Expand]Popup Menus
   [Expand]Saving and Restoring Layouts
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Map Control
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]PDF Viewer
  [Expand]TreeMap Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Report Server
[Expand]eXpressApp Framework
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Sorting Overview

This document provides basic information on sorting data in Grid Control. The sorting functionality is available for Grid Views (including their descendants), Layout Views and Card Views.

Views allow you to sort data by an unlimited number of columns. When sorting is applied to a View, records are re-arranged to meet current sort settings.

Grid Control manages the automatic sorting for all types of data sources. The grid sorts the data itself and does not delegate this functionality to the underlying data source.


In Server Mode, the data sorting functionality is delegated to the data source.

The following subsections give more details on the Grid Control's built-in sorting functionality.

Expanded Sorting at Runtime

To sort data against a Grid View column or to change the column's sort order, an end-user can click its column header. The column's current sort order is indicated by the sort glyph (a small arrow displayed at the column header's right edge). If data is sorted in ascending order, the sort glyph represents an up-arrow. When sorting in descending order, the sort glyph is displayed as a down-arrow. If the column isn't sorted, the sort glyph is hidden. In the following image, data is sorted against the Supplier ID column in ascending order and against the Unit Price column in descending order.

In Card Views, sorting can be applied to columns (card fields) through the Customization Button. Clicking this button invokes the dropdown that lists available columns. Clicking a Sort button for a column in this dropdown is similar to clicking a column header in a Grid View.

If sorting isn't applied, clicking the column header (Sort button in Card Views) sorts data by column contents in ascending order. If sorting is already applied to the column, subsequent clicks reverse the current sort order. Note: a regular click on a column header (Sort button) clears the sort settings of all other columns. To preserve the existing sort settings of other columns, hold the SHIFT key down while clicking. This can be useful when you need to apply sorting to multiple columns at the same time. You can clear the sorting of a column by clicking its header (Sort button) while pressing the CTRL key.

In Grid Views, end-users can also apply sorting to columns through the column header context menu.

This menu's availability is controlled by the View's GridOptionsMenu.EnableColumnMenu property. Menu content can be customized as described in the Implementing Custom Behavior for Popup Menus section.

In Grid Views, the GridOptionsCustomization.AllowSort property can be used to prevent an end-user from changing the current sort settings of any of the columns in a View. The OptionsColumn.AllowSort option can be used to prevent the sort settings of a particular column being changed. This option is available for columns in grid and Card Views. Note that if sorting is disabled for a column, records cannot be grouped by this column. For more information on grouping, refer to the Grouping Overview document.

Regardless of the values of the grid's GridOptionsCustomization.AllowSort and the column's OptionsColumn.AllowSort properties, the sort order can always be changed through code.

Expanded Sorting using Code

To apply sorting to a column(s), utilize the following methods.

To remove sorting against a column, do the following.

The ColumnView.SortInfo property maintains a collection of the sorted and grouping columns for a View. Thus, adding elements to and removing them from the collection will apply or remove sorting against a specific column. This column's GridColumn.SortOrder property will reflect the changes made to the sort settings automatically.

Similarly, when a column's GridColumn.SortOrder property is set to ColumnSortOrder.Ascending or ColumnSortOrder.Descending, an item is added to the ColumnView.SortInfo collection. Setting the GridColumn.SortOrder property to ColumnSortOrder.None will remove the corresponding item from the ColumnView.SortInfo.

The following code shows how to sort data against the Country column in ascending order.

Changing the sort order of a column in code does not clear the sort settings of other columns. So the code above just adds the Country column to the collection of previously sorted columns. In some cases, you may want to remove the sorting against the other columns before applying new sorting.

The following code shows how to apply sorting to the Country and City columns using the ColumnView.SortInfo collection, while removing all sorting applied to other columns. For this purpose, the GridColumnSortInfoCollection.ClearAndAddRange method is used. This method deletes the existing sorting items and then adds new ones.

The equivalent code of applying sorting to two columns using their GridColumn.SortOrder properties is as follows.

The ColumnView.ClearSorting method used in the example clears the sorted column collection. The ColumnView.BeginSort and ColumnView.EndSort methods are required to prevent excessive sorting of data when the sort settings for multiple columns are changed at the same time (see the PREVENTING EXCESSIVE DATA RE-SORTING subsection).

The order of the columns in the sorted column collection is important since it defines the order in which data is sorted. First, data is sorted by the collection's first column. Then, data is sorted by the second column and so on.

All the sorted columns can be accessed using the read-only ColumnView.SortedColumns collection. This collection, unlike the ColumnView.SortInfo collection, does not store grouping columns. A column's position in the ColumnView.SortedColumns collection is specified by the GridColumn.SortIndex property. You can use this property to cancel sorting, apply sorting in ascending order for this column, or change the column's position amongst other sort columns.

When the sort settings for a column are changed, the grid does the following:

Expanded Preventing Excessive Data Re-Sorting

Suppose your code consists of multiple statements, each of which modifies the sort settings of one or more columns. For instance, in the example above, the code between the try and finally keywords removes any sorting for all columns first, and then applies sorting to the Country and after that to City columns. By default, the grid re-sorts its data after each call to a function (property) that modifies the sort settings and therefore data in the example would be re-sorted three times by default.

To prevent excessive data re-sorting, the ColumnView.BeginSort and ColumnView.EndSort methods can be used. In this case, data is sorted only after a call to the ColumnView.EndSort method and this takes into account all the recent changes to the sort settings.

Each call to ColumnView.BeginSort, must have a corresponding ColumnView.EndSort call. To ensure this, a try...finally block is used.

The Batch Modifications Overview section provides information on batch modification methods provided by XtraGrid. These allow you to prevent excessive visual updates, excessive sorting and selection updates, and excessive summary item and style condition collection updates.

The GridColumnSortInfoCollection.ClearAndAddRange method (used in one of the examples above) calls methods that prevent superfluous data re-sorting internally. So there is no need to enclose this method with ColumnView.BeginSort and ColumnView.EndSort.

If you enclose the code that changes sort settings within ColumnView.BeginSort and ColumnView.EndSort methods, then data is sorted and the ColumnView.EndSorting event is only raised after the ColumnView.EndSort method is called.

Expanded Sorting by Values and Display Text. Custom Sorting

Grid Control supports the sorting of data by its edit values, display values and custom sorting. The GridColumn.SortMode property allows you to specify the required sort mode.

By default, the GridColumn.SortMode property is set to ColumnSortMode.Default. In this case, the column's data will be sorted by the edit values or display values according to the type of the in-place editor assigned to the column.

For columns that use the LookUpEdit and ImageComboBoxEdit in-place editors, data is sorted by the displayed values (the strings displayed within column cells).

For other columns, their data is sorted by edit values (these are synchronized with the bound data source values). For some editors (TextEdit, ComboBoxEdit, etc.), however, the edit values match the display values.

To force a column to use a specific sort mode, explicitly set the GridColumn.SortMode property to ColumnSortMode.Value or ColumnSortMode.DisplayText.

The custom sorting mode enables you to implement custom sorting logic if the standard sorting algorithms don't suit your needs. To enable this mode, set the GridColumn.SortMode property to ColumnSortMode.Custom and handle the ColumnView.CustomColumnSort event.

On each event call, two values should be compared. The result of a custom comparison should be set to the event's CustomColumnSortEventArgs.Result parameter. In addition, the CustomColumnSortEventArgs.Handled parameter must be set to true to indicate that the comparison was handled. If this parameter is left set to false, the default comparison mechanism will be invoked to compare the values.

Expanded See Also

How would you rate this topic?​​​​​​​