CustomColumnSortEventArgs Class
Namespace: DevExpress.Xpf.Grid
Assembly: DevExpress.Xpf.Grid.v18.2.dll
Declaration
public class CustomColumnSortEventArgs :
EventArgs
Public Class CustomColumnSortEventArgs
Inherits EventArgs
To learn more, see Sorting Modes and Custom Sorting.
Example
This example demonstrates how to implement custom sorting in the Grid Control. To do this, handle the GridControl.CustomColumnSort event, assign your custom sorted list to the e.Result property and set the e.Handled property to True.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CustomSorting {
public class SchedulerData {
const int daysInWeek = 7;
string[] employees = new string[] { "Jane", "Martin", "John", "Jack", "Amanda", "Carmen", "Wins", "Todd", "Ashley" };
Random rnd = new Random();
public List<SchedulerItem> Items { get; set; }
public SchedulerData() {
Items = new List<SchedulerItem>();
GenerateRandomData();
}
void GenerateRandomData() {
for (int i = 0; i < daysInWeek; i++) {
int e1 = rnd.Next(employees.Length - 1);
int e2 = e1 + 1;
string day = DateTime.Today.AddDays(i).DayOfWeek.ToString();
Items.Add(new SchedulerItem() { Day = day, Employee = employees[e1] });
Items.Add(new SchedulerItem() { Day = day, Employee = employees[e2] });
}
}
}
public class SchedulerItem {
public string Day { get; set; }
public string Employee { get; set; }
}
}
using System.Windows;
using System.Collections.Generic;
using DevExpress.Xpf.Grid;
using CustomSorting;
using System;
namespace CustomSorting {
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
DataContext = new SchedulerData();
}
void OnCustomColumnSort(object sender, CustomColumnSortEventArgs e) {
if (e.Column.FieldName == "Day") {
int dayIndex1 = GetDayIndex((string)e.Value1);
int dayIndex2 = GetDayIndex((string)e.Value2);
e.Result = dayIndex1.CompareTo(dayIndex2);
e.Handled = true;
}
}
int GetDayIndex(string day) {
return (int)Enum.Parse(typeof(DayOfWeek), day);
}
}
}
<Window x:Class="CustomSorting.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
Title="Custom Column Sort" Height="300" Width="300" >
<Grid>
<dxg:GridControl ItemsSource="{Binding Items}" Grid.Row="1" CustomColumnSort="OnCustomColumnSort">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Day" GroupIndex="0" SortMode="Custom" />
<dxg:GridColumn FieldName="Employee" />
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView />
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
Imports System.Windows
Imports System.Collections.Generic
Imports DevExpress.Xpf.Grid
Imports CustomSorting
Imports System
Namespace CustomSorting
Partial Public Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
DataContext = New SchedulerData()
End Sub
Private Sub OnCustomColumnSort(ByVal sender As Object, ByVal e As CustomColumnSortEventArgs)
If e.Column.FieldName = "Day" Then
Dim dayIndex1 As Integer = GetDayIndex(DirectCast(e.Value1, String))
Dim dayIndex2 As Integer = GetDayIndex(DirectCast(e.Value2, String))
e.Result = dayIndex1.CompareTo(dayIndex2)
e.Handled = True
End If
End Sub
Private Function GetDayIndex(ByVal day As String) As Integer
Return DirectCast(System.Enum.Parse(GetType(DayOfWeek), day), Integer)
End Function
End Class
End Namespace
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Namespace CustomSorting
Public Class SchedulerData
Private Const daysInWeek As Integer = 7
Private employees() As String = { "Jane", "Martin", "John", "Jack", "Amanda", "Carmen", "Wins", "Todd", "Ashley" }
Private rnd As New Random()
Public Property Items() As List(Of SchedulerItem)
Public Sub New()
Items = New List(Of SchedulerItem)()
GenerateRandomData()
End Sub
Private Sub GenerateRandomData()
For i As Integer = 0 To daysInWeek - 1
Dim e1 As Integer = rnd.Next(employees.Length - 1)
Dim e2 As Integer = e1 + 1
Dim day As String = Date.Today.AddDays(i).DayOfWeek.ToString()
Items.Add(New SchedulerItem() With {.Day = day, .Employee = employees(e1)})
Items.Add(New SchedulerItem() With {.Day = day, .Employee = employees(e2)})
Next i
End Sub
End Class
Public Class SchedulerItem
Public Property Day() As String
Public Property Employee() As String
End Class
End Namespace
See Also