Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [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
   [Expand]Grouping
   [Collapse]Sorting
     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]Summaries
   [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]Hints
   [Expand]Popup Menus
   [Expand]Saving and Restoring Layouts
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]Examples
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Diagrams
  [Expand]Gauges
  [Expand]Map Control
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]Printing-Exporting
  [Expand]PDF Viewer
   Reporting
  [Expand]Snap
  [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]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

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

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