Grouping
Data Grid can group data against one or multiple columns. Data grouping combines rows with identical column values into the same data groups.
A user can group data as follows:
Drag a column header to the group panel.
Right-click a column header and select the "Group By This Column" command.
To ungroup data, a user can do one of the following actions:
Drag a group column header from the group panel.
Right-click a group column header and select the "Ungroup" command.
When you apply grouping by a column, this column's data is sorted. A user can click the group column header to toggle the sort order.
Group Panel
A group panel displays headers of group columns. A user can drag a column header onto the group panel to group by this column.
Use the DataGrid's ShowGroupPanel
property to specify the visibility of the group panel. A user can hide and then restore the group panel using the context menu for column headers:
Group Columns
When you group by a column, this column is moved from the grid to the group panel. Set the ShowGroupedColumns
property to true
to display group columns in the group panel and grid at the same time. The following image illustrates the ShowGroupedColumns
setting:
Data in group columns is sorted in ascending or descending order. If a column does not support sorting, data cannot be grouped by this column as well.
Group in Code
To group data by a column(s) in code, do the following:
- Sort this column(s), and position it (them) at the beginning of the sorted column collection. You can sort a column using the
SortIndex
andSortDirection
properties. See Sorting for more information. - Set the
DataGridControl.GroupCount
property to the number of group columns.
The following code groups data by two columns.
GridColumn column1 = dataGrid.Columns["EmploymentType"];
GridColumn column2 = dataGrid.Columns["Position"];
if(column1 != null && column2 != null)
{
dataGrid.BeginDataUpdate();
column1.SortIndex = 0;
column2.SortIndex = 1;
column2.SortDirection = System.ComponentModel.ListSortDirection.Descending;
dataGrid.GroupCount = 2;
dataGrid.EndDataUpdate();
}
This example uses the BeginDataUpdate
and EndDataUpdate
methods to prevent superfluous updates when you change multiple group/sort settings. The Data Grid is only updated after the EndDataUpdate
method call.
Without using the BeginDataUpdate
and EndDataUpdate
methods, the Data Grid is updated after each change to any group/sort setting.
Each call to the BeginDataUpdate
method must be followed by a call to the EndDataUpdate
method.
Group Rows
Group rows are used to form a hierarchy when data is grouped. They display values of corresponding group columns. Group rows do not exist in the bound item source. See the following topic for information on row identification: Rows - Identify and Get Rows.
Data Grid exposes the following API that helps you obtain and expand/collapse group rows:
AutoExpandAllGroups
— Specifies whether to automatically expand group rows after each data grouping operation.CollapseAllGroups
— Collapses all group rows.CollapseGroupRow
— Collapses a group row.ExpandAllGroups
— Expands all group rows.ExpandGroupRow
— Expands a group row.IsGroupRow
— Specifies whether a specific row is a group row.IsGroupRowExpanded
— Specifies whether a group row is expanded.
Group Row Values
Use the GetGroupRowValue
method to retrieve a group value of a specific group row.
Group Row Text
The default text displayed in group rows is specified by values of corresponding group columns. The CustomGroupDisplayText
event allows you to specify custom display text for group rows.
This event fires repeatedly for each group row. Use the event's arguments to identify the currently processed group row and its value. To specify a custom display value, set the DisplayText
event argument.