[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
       Tutorial: Custom Grouping Algorithms
       Tutorial: Group Row API
       Tutorial: Grouping Basics
     [Expand]Filter and Search
     [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 Grouping Algorithms

This walkthrough is a transcript of the Custom Grouping Algorithms video available on the DevExpress YouTube Channel.

By default, data groups are formed by putting identical values into each group. Although the grid provides a few other built-in algorithms, sometimes you need to implement your own logic, which is done using the GridView.CustomColumnGroup event. You will learn how to handle this event in order to change a numeric column's data grouping logic so that groups contain ranges of values starting with 0 to 99.99, then 100 to 199.99 and so on.

Expanded Default Behavior

When you group the grid's data against the Order Sum column, by default, groups are formed based on unique values in that column.

Expanded Implementing Custom Grouping Logic

Switch to design-time and first of all, enable custom sorting mode for this column by setting the GridColumn.SortMode property to ColumnSortMode.Custom.

After that, select the grid View and handle its GridView.CustomColumnGroup event. The column being processed is specified by the event's CustomColumnSortEventArgs.Column parameter. The event handler compares two rows with values specified by the CustomColumnSortEventArgs.Value1 and CustomColumnSortEventArgs.Value2 parameters. Since you combine values into groups corresponding to hundreds, divide the values by 100 and compare the results. To place all values above 1000 into the same group, the handler assumes that results are equal if they are both greater than 10. The CustomColumnSortEventArgs.Result parameter is set to 0 in that case. Other values undergo standard comparison.

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

To better illustrate how this code works, the grouping column values should be visible even when data is grouped. To enable this behavior, expand GridView.OptionsView and set the GridOptionsView.ShowGroupedColumns option to true.

Now run the application and group grid data by the Order Sum column. As a result, rows are combined into the specified intervals. However, group rows display the first value in a corresponding group, thus misleading end-users. This can also be fixed.

Expanded Providing Custom Display Text for Group Rows

Return to design time and subscribe to the ColumnView.CustomColumnDisplayText event. Check the event's CustomColumnDisplayTextEventArgs.Column and CustomColumnDisplayTextEventArgs.IsForGroupRow parameters. The first parameter specifies the column currently being processed, while the second parameter indicates whether the event is called to customize group row display text. The CustomColumnDisplayTextEventArgs.GroupRowHandle parameter returns the currently processed group row. To change the group row's display text, use the event's CustomColumnDisplayTextEventArgs.DisplayText parameter.

Run the application and group data by the Order Sum column once again. Group rows will now display custom text that correctly identifies the intervals.

Expanded See Also

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