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: 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

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