[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: Custom Sorting and Non-Sortable Columns

This walkthrough is a transcript of the Custom Sorting and Non-Sortable Columns video available on the DevExpress YouTube Channel.

In this tutorial, you will learn how to implement a custom sorting algorithm to sort the Birth Date column's values without taking into account the year part of the date, thus obtaining a properly sorted list of all birthdays in an organization. You will also learn the technique to enable sorting against columns containing non-sortable data, such as images.

Expanded Default Behavior

If you click the Birth Date column header, the default algorithm sorts dates taking into account years first, then months and then days. As you see, the list displays a January birthday, then one in May, and then a January date again, because they are sorted by years.

Expanded Implementing Custom Sorting Logic

This tutorial implements a custom sorting logic that would ignore the year part and give a list of birthdays sorted by months and dates only.

First of all, set the column's GridColumn.SortMode property to ColumnSortMode.Custom.

After that, select the grid View and handle its ColumnView.CustomColumnSort event.

The column being processed is specified by the event's CustomColumnSortEventArgs.Column parameter. The event handler compares two values specified using the CustomColumnSortEventArgs.Value1 and CustomColumnSortEventArgs.Value2 parameters. The result of a custom comparison is set to the CustomColumnSortEventArgs.Result parameter. This example compares birthday months. If the first month number is greater than the second, the Result parameter is set to 1. If the first month number is less than the second, set the Result parameter to -1. In case values are equal, compare days.

The event's CustomColumnSortEventArgs.Handled parameter is set to true to prevent the default comparison mechanism from being invoked after event execution.

Run the application and click the Birth Date column header. Cell values are now sorted based on a month and date comparison and the sort order ignores years.

Expanded Implementing Sorting Algorithm for Non-Sortable Columns

The GridControl can contain columns that display non-sortable data, such as images.

The Photo column displays images using a PictureEdit in-place editor, and nothing happens when you click its header. However, you can enable data sorting for this column too.

First of all, select the Photo column, expand its GridColumn.OptionsColumn property and set the OptionsColumn.AllowSort setting to true. Then, set its GridColumn.SortMode property to ColumnSortMode.Custom.

Add the following code to the ColumnView.CustomColumnSort event handler. When the Photo column's data is sorted, the data will actually be sorted against the First Name column. Column values are accessed on the data source level using the event's CustomColumnSortEventArgs.ListSourceRowIndex1 and CustomColumnSortEventArgs.ListSourceRowIndex2 parameters. The CustomColumnSortEventArgs.Handled parameter is set to true to indicate that the comparison operation was handled.

Run the application and click the Photo column header. Grid data is now sorted against the values in the FirstName column.

Expanded See Also

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