GridControl.CustomColumnGroup Event
Provides the ability to group data using custom rules.
Namespace: DevExpress.Xpf.Grid
Assembly: DevExpress.Xpf.Grid.v18.2.dll
Declaration
public event CustomColumnSortEventHandler CustomColumnGroup
Public Event CustomColumnGroup As CustomColumnSortEventHandler
Event Data
The CustomColumnGroup event's data class is CustomColumnSortEventArgs.
The following properties provide information specific to this event:
Property |
Description |
Column |
Gets the column whose values are being compared.
|
Handled |
Gets or sets whether a comparison operation is handled and no default processing is required.
|
ListSourceRowIndex1 |
Gets the index of the first of the two rows being compared in the data source.
|
ListSourceRowIndex2 |
Gets the index of the second of the two rows being compared in the data source.
|
Result |
Gets or sets the result of a custom comparison.
|
SortOrder |
Gets the sort order applied to the column.
|
Source |
Gets the grid control that raised the event.
|
Value1 |
Gets the first value being compared.
|
Value2 |
Gets the second value being compared.
|
If the built-in grouping modes don’t suit your requirements, you can implement custom logic. To do this, set the ColumnBase.SortMode property to ‘Custom’, and handle the CustomColumnGroup event. To change the text displayed within group rows by default, handle the GridControl.CustomGroupDisplayText event.
Note
The CustomColumnGroup event does not work in Server Mode.
Example
This example shows how to group rows using custom rules. When grouping by the 'Unit Price' column, the rows in this column that have values between 0 and 10 should be combined into a single group. Rows whose values fall between 10 and 20 should be combined into another group, etc.
<Window x:Class="DXGrid_CustomGrouping.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
Title="Window1" Height="300" Width="509">
<Grid>
<dxg:GridControl x:Name="grid"
CustomColumnGroup="grid_CustomColumnGroup"
CustomGroupDisplayText="grid_CustomGroupDisplayText"
ItemsSource="{Binding ListPerson}"
>
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="FirstName" />
<dxg:GridColumn FieldName="LastName" />
<dxg:GridColumn FieldName="UnitPrice" SortMode="Custom" GroupIndex="0">
<dxg:GridColumn.EditSettings>
<dxe:SpinEditSettings DisplayFormat="c2" />
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView AutoWidth="True" />
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</Window>
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Linq
Imports System.Text
Namespace DXGrid_CustomGrouping
Public Class MyViewModel
Public Sub New()
CreateList()
End Sub
Public Property ListPerson() As ObservableCollection(Of Person)
Private Sub CreateList()
ListPerson = New ObservableCollection(Of Person)()
For i As Integer = 0 To 99
Dim p As New Person(i)
ListPerson.Add(p)
Next i
End Sub
End Class
Public Class Person
Public Sub New()
End Sub
Public Sub New(ByVal i As Integer)
FirstName = "FirstName" & i
LastName = "LastName" & i
UnitPrice = i
End Sub
Private _firstName As String
Public Property FirstName() As String
Get
Return _firstName
End Get
Set(ByVal value As String)
_firstName = value
End Set
End Property
Public Property LastName() As String
Private _age As Integer
Public Property UnitPrice() As Integer
Get
Return _age
End Get
Set(ByVal value As Integer)
_age = value
End Set
End Property
End Class
End Namespace
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Collections
Imports DevExpress.Xpf.Grid
Namespace DXGrid_CustomGrouping
''' <summary>
''' Interaction logic for Window1.xaml
''' </summary>
Partial Public Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
Me.DataContext = New MyViewModel()
End Sub
Private Sub grid_CustomColumnGroup(ByVal sender As Object, ByVal e As CustomColumnSortEventArgs)
If e.Column.FieldName = "UnitPrice" Then
Dim x As Double = Math.Floor(Convert.ToDouble(e.Value1) / 10)
Dim y As Double = Math.Floor(Convert.ToDouble(e.Value2) / 10)
Dim res As Integer = Comparer.Default.Compare(x, y)
If x > 9 AndAlso y > 9 Then
res = 0
End If
e.Result = res
e.Handled = True
End If
End Sub
Private Sub grid_CustomGroupDisplayText(ByVal sender As Object, ByVal e As CustomGroupDisplayTextEventArgs)
If e.Column.FieldName <> "UnitPrice" Then
Return
End If
Dim groupLevel As Integer = grid.GetRowLevelByRowHandle(e.RowHandle)
If groupLevel <> e.Column.GroupIndex Then
Return
End If
Dim interval As String = IntervalByValue(e.Value)
e.DisplayText = interval
End Sub
' Gets the text that represents the interval which contains the specified value.
Private Function IntervalByValue(ByVal val As Object) As String
Dim d As Double = Math.Floor(Convert.ToDouble(val) / 10)
Dim ret As String = String.Format("{0:c} - {1:c} ", d * 10, (d + 1) * 10)
If d > 9 Then
ret = String.Format(">= {0:c} ", 100)
End If
Return ret
End Function
End Class
End Namespace
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace DXGrid_CustomGrouping {
public class MyViewModel {
public MyViewModel() {
CreateList();
}
public ObservableCollection<Person> ListPerson { get; set; }
void CreateList() {
ListPerson = new ObservableCollection<Person>();
for (int i = 0; i < 100; i++) {
Person p = new Person(i);
ListPerson.Add(p);
}
}
}
public class Person {
public Person() {
}
public Person(int i) {
FirstName = "FirstName" + i;
LastName = "LastName" + i;
UnitPrice = i ;
}
string _firstName;
public string FirstName {
get { return _firstName; }
set {
_firstName = value;
}
}
public string LastName { get; set; }
int _age;
public int UnitPrice {
get { return _age; }
set { _age = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using DevExpress.Xpf.Grid;
namespace DXGrid_CustomGrouping {
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
this.DataContext = new MyViewModel();
}
private void grid_CustomColumnGroup(object sender,
CustomColumnSortEventArgs e) {
if (e.Column.FieldName == "UnitPrice") {
double x = Math.Floor(Convert.ToDouble(e.Value1) / 10);
double y = Math.Floor(Convert.ToDouble(e.Value2) / 10);
int res = Comparer.Default.Compare(x, y);
if (x > 9 && y > 9) res = 0;
e.Result = res;
e.Handled = true;
}
}
private void grid_CustomGroupDisplayText(object sender,
CustomGroupDisplayTextEventArgs e) {
if (e.Column.FieldName != "UnitPrice") return;
int groupLevel = grid.GetRowLevelByRowHandle(e.RowHandle);
if (groupLevel != e.Column.GroupIndex) return;
string interval = IntervalByValue(e.Value);
e.DisplayText = interval;
}
// Gets the text that represents the interval which contains the specified value.
private string IntervalByValue(object val) {
double d = Math.Floor(Convert.ToDouble(val) / 10);
string ret = string.Format("{0:c} - {1:c} ", d * 10, (d + 1) * 10);
if (d > 9) ret = string.Format(">= {0:c} ", 100);
return ret;
}
}
}
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CustomColumnGroup event.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
See Also