Besides the built-in grouping functionality you are able to use a programmatic approach to group the data in the RadGridView. This is achieved via the GroupDescriptors property of the RadGridView. This collection of IGroupDescriptor objects allows you to use descriptors (GroupDescriptors or ColumnGroupDescriptors) which define the grouping criteria and the group's sorting direction for the data that is bound to the RadGridView. As this is a collection you are able not only to add, but to remove or clear the entries in it too.
When you add a new descriptor to the collection, the data is automatically grouped according it. To learn how to create and configure descriptors take a look at the following example.
CopyC#
GroupDescriptor descriptor = new GroupDescriptor();
descriptor.Member = "Country";
descriptor.SortDirection = ListSortDirection.Ascending;
CopyVB.NET
Dim descriptor As New GroupDescriptor()
descriptor.Member = "Country"
descriptor.SortDirection = ListSortDirection.Ascending
The Member property defines the property, by which the data will be grouped, and the SortDirection property allows you to define the direction by which the groups will be sorted.
And now you can create a descriptor:
CopyXAML
<telerik:GroupDescriptor Member="Country"
SortDirection="Ascending" />To use the created descriptor to sort the data in the RadGridView you have to add it to the GroupDescriptors collection.
CopyXAML
<telerik:RadGridView x:Name="radGridView"
AutoGenerateColumns="False">
<telerik:RadGridView.GroupDescriptors>
<telerik:GroupDescriptor Member="Country"
SortDirection="Ascending" />
</telerik:RadGridView.GroupDescriptors>
...
</telerik:RadGridView>.
CopyC#
this.radGridView.GroupDescriptors.Add( descriptor );
CopyVB.NET
Me.radGridView.GroupDescriptors.Add(descriptor)
After the application is run with this descriptor defined, the RadGridView data will be grouped by the Country property and will look as if you have dragged and dropped the Country column into the grouping area.
Except the Member and the SortDirection properties, the GroupDescriptor exposes two more. The DisplayContent property allows you to change the content of the rectangle representing the group that appears in the grouping area.
CopyXAML
<telerik:GroupDescriptor Member="Country"
SortDirection="Ascending"
DisplayContent="Country Group" />
CopyC#
GroupDescriptor descriptor = new GroupDescriptor();
descriptor.Member = "Country";
descriptor.SortDirection = ListSortDirection.Ascending;
descriptor.DisplayContent = "Country Group";
CopyVB.NET
Dim descriptor As New GroupDescriptor()
descriptor.Member = "Country"
descriptor.SortDirection = ListSortDirection.Ascending
descriptor.DisplayContent = "Country Group"
Tip |
|---|
| In the group rows you are able to display aggregate functions, which display information about the data contained in the group. To learn more about how to add aggregate functions to the group rows take a look at the Group Aggregates topic. |
An alternative way of manipulating the group descriptors of RadGridView is the new (added in Q3 2010) ColumnGroupDescriptor class. It has three important properties:
- Column - the column that will be grouped
- DisplayContent - allows you to change the content of the rectangle representing the group that appears in the grouping area.
- SortDirection - allows you to define the direction by which the groups will be sorted
As you see the last two properties are similar to the same properties of the GroupDescriptor class.
Note |
|---|
As of Q3 2011 you can add the ColumnGroupDescriptor both in the code behind and XAML. With all the prior versions you can only set it in code behind. |
Here is a code snippet which adds a ColumnGroupDescriptor to RadGridView which groups by the Name column in descending order:
CopyXAML
<telerik:ColumnGroupDescriptor Column="{Binding Columns[\Name\], ElementName=clubsGrid}"
SortDirection="Descending" />
CopyC#
this.clubsGrid.GroupDescriptors.Add(new ColumnGroupDescriptor()
{
Column = this.clubsGrid.Columns["Name"],
SortDirection = ListSortDirection.Descending
});
CopyVB.NET
Me.clubsGrid.GroupDescriptors.Add(New ColumnGroupDescriptor() With { _
.Column = Me.clubsGrid.Columns("Name"), _
.SortDirection = ListSortDirection.Descending _
}) Caution |
|---|
As of Q3 2010, when the user groups from the UI - a ColumnGroupDescriptor (instead of GroupDescriptor) is added to the GroupDescriptors collection of RadGridView |
See Also