[Expand]General Information
[Collapse]WinForms Controls
  .NET Core Support
 [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
   [Collapse]Get Started With Data Grid and Views
     [Expand]Appearance and Conditional Formatting
     [Expand]Data Editing
     [Expand]Hit Information
     [Expand]Data Binding and Working with Columns
     [Expand]Grid View - Columns, Rows and Cells
     [Expand]Banded Views
     [Expand]WinExplorer View
     [Expand]Tile View
     [Expand]Filter and Search
       Tutorial: Custom Sorting and Non-Sortable Columns
       Tutorial: Data Sorting Basics
       Tutorial: Sorting by Values or Display Text
     [Expand]Split Presentation
     [Expand]Row Preview Sections
   [Expand]Data Binding
    Unbound Columns
   [Expand]Data Editing and Validation
   [Expand]Filter and Search
   [Expand]Focus and Selection Handling
    Format Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
    Split Presentation
    Row Preview Sections
   [Expand]Batch Modifications
    Hit Information
    Popup Menus
   [Expand]Save and Restore Layout
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Property Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Gantt Control
  [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]Sunburst Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Tutorial: Data Sorting Basics

This walkthrough is a transcript of the Data Sorting Basics video available on the DevExpress YouTube Channel.

This tutorial will guide you through the data sorting features available to end-users at runtime. You will also see how to manipulate data sorting at design time and in code.

Expanded Default Behavior

To sort data against a column, click this column's header. The grid will sort values in ascending order. The order is indicated by the glyph displayed at the column header's right edge.

Click the same column header to reverse the sort order. Note that the sort glyph is now reversed as well.

If you click another column header, all previously applied data sorting is cleared. To preserve the existing sort settings, and thus sort against multiple columns, hold the SHIFT key down while clicking.Sorting by a second column only makes sense when you have repeated values in the first column.

If you use the Sort Ascending or Sort Descending items in the column header menu, the previously applied data sorting is also preserved.

To disable data sorting against a specific column, hold the CTRL key and click this column's header. The same can be accomplished using the Clear Sorting item in the column header menu. To clear all data sorting conditions, use the Clear All Sorting item.

Expanded Sorting at Design Time

You can sort grid data at design time using column header context menus or the Property Grid. Click the Project Name and Status column headers while holding down the SHIFT key and set their GridColumn.SortOrder property to ColumnSortOrder.Ascending.

Run the application. Grid data is sorted against the Project Name and then the Status column.

Return to design time and clear the sort settings of these columns by setting the GridColumn.SortOrder property to ColumnSortOrder.None.

Expanded Restricting End-User Capabilities

To disable end-user data sorting against a column, set the column's OptionsColumn.AllowSort option to false.

Run the application. By right-clicking the Project Name column header, you will find out if the Sort Ascending or Sort Descending items are disabled. Note that you can still sort grid data against other columns.

To prevent end-user sorting against all columns in a centralized way, select the grid View, expand the GridView.OptionsCustomization property and disable the GridOptionsCustomization.AllowSort option.

Run the application again to ensure that grid data cannot be sorted.

Expanded Sorting in Code

The next step is to see how to sort grid data in code. Note that this works regardless of the View's GridOptionsCustomization.AllowSort and the column's OptionsColumn.AllowSort options.

Sort data against the Project Name and Status columns in a button's Click event handler. First, obtain the GridColumn objects to which sorting should be applied. Clear existing sort settings by calling the View's ColumnView.ClearSorting method. Then, use the GridColumn.SortOrder property to set required sort orders. Note that the GridControl will re-sort its data after each statement – three times in this example. To prevent excessive updates, use the ColumnView.BeginSort and ColumnView.EndSort methods. In this case, the grid's data will be re-sorted only once.

Run the application and click the button. As a result, grid data is sorted against the Project Name and then Status columns.

Another way to apply sorting to grid columns is to add corresponding items to the View's ColumnView.SortInfo collection by calling the GridColumnSortInfoCollection.ClearAndAddRange method. This method clears the existing sorting, adds new GridColumnSortInfo objects passed as parameters and only updates the View once so that you don't need the ColumnView.BeginSort and ColumnView.EndSort methods.

Run the application again and click the button to see the result.

Expanded See Also

Is this topic helpful?​​​​​​​