Edit this page

Group Aggregates

This article will discuss the following topics:

Figure 1: RadGridView With Group Aggregates

RadGridView With Group Aggregates

Aggregate Functions

When you are using programmatic grouping, you can add aggregate functions to the group rows. These functions allow you to display information about the data in the group such as first item, last item, count of items etc.

The available functions are:

  • AverageFunction – returns the average of the values in a group.

  • CountFunction – returns the number of all items in a group.

  • FirstFunction – returns first element from a group according to the current sorting.

  • LastFunction – returns last element from a group according to the current sorting.

  • MaxFunction – returns the Max of the values in a group.

  • MinFunction – returns the Min of the values in a group.

  • SumFunction – returns the Sum of all values for the group.

You can also create your own custom functions similar to these by inheriting Telerik.Windows.Data.EnumerableAggregateFunction class. An example can be found here.

Each aggregate function has a caption and a result, which are displayed next to the group title.

To use the aggregate functions you have to declare them and add them to the AggregateFunctions collection of the GroupDescriptor.

An alternative way of manipulating the group descriptors of RadGridView is the new (added in Q3 2010) ColumnGroupDescriptor class. You can directly define ColumnGroupDescriptor and add the AggregateFunctions for the respective column. Please note ColumnGroupDescriptor is associated with a specific column and that is why there is not a need for it to also expose the option do define AggregateFunctions as GroupDescriptor.

Example 1: Defining a GroupDescriptor with AggregateFunction

<telerik:RadGridView x:Name="radGridView"
                         AutoGenerateColumns="False">
    <telerik:RadGridView.GroupDescriptors>
        <telerik:GroupDescriptor Member="Country"
                                     SortDirection="Ascending">
            <telerik:GroupDescriptor.AggregateFunctions>
                <telerik:CountFunction Caption="Entries count: " />
            </telerik:GroupDescriptor.AggregateFunctions>
        </telerik:GroupDescriptor>
    </telerik:RadGridView.GroupDescriptors>
    ...
</telerik:RadGridView>

Example 2: Defining a GroupDescriptor with AggregateFunction programmatically

CountFunction f = new CountFunction();
f.Caption = "Entries Count: ";
GroupDescriptor countryDescriptor = new GroupDescriptor();
countryDescriptor.Member = "Country";
countryDescriptor.SortDirection = ListSortDirection.Ascending;
countryDescriptor.AggregateFunctions.Add( f );
this.radGridView.GroupDescriptors.Add( countryDescriptor );
Dim f As New CountFunction()
f.Caption = "Entries Count: "
Dim countryDescriptor As New GroupDescriptor()
countryDescriptor.Member = "Country"
countryDescriptor.SortDirection = ListSortDirection.Ascending
countryDescriptor.AggregateFunctions.Add(f)
Me.radGridView.GroupDescriptors.Add(countryDescriptor)

You can add more than one aggregate function to AggregateFunctions collection and it will be visualized after the first one.

The FirstFunction will display the value returned by overriding ToString() method of your business object.

Example 3: Defining a GroupDescriptor with multiple AggregateFunctions

<telerik:RadGridView x:Name="radGridView"
                         AutoGenerateColumns="False">
    <telerik:RadGridView.GroupDescriptors>
        <telerik:GroupDescriptor Member="Country"
                                     SortDirection="Ascending">
            <telerik:GroupDescriptor.AggregateFunctions>
                <telerik:CountFunction Caption="Entries count: " />
                <telerik:FirstFunction Caption="First entry: " />            
            </telerik:GroupDescriptor.AggregateFunctions>
        </telerik:GroupDescriptor>
    </telerik:RadGridView.GroupDescriptors>
    ...
</telerik:RadGridView>

Example 4: Defining a GroupDescriptor with multiple AggregateFunctions programmatically

CountFunction f = new CountFunction();
f.Caption = "Entries Count: ";
FirstFunction f1 = new FirstFunction();
f.Caption = "FirstEntry: ";
GroupDescriptor countryDescriptor = new GroupDescriptor();
countryDescriptor.Member = "Country";
countryDescriptor.SortDirection = ListSortDirection.Ascending;
countryDescriptor.AggregateFunctions.Add( f );
countryDescriptor.AggregateFunctions.Add( f1 );
this.radGridView.GroupDescriptors.Add( countryDescriptor );
Dim f As New CountFunction()
f.Caption = "Entries Count: "
Dim f1 As New FirstFunction()
f.Caption = "FirstEntry: "
Dim countryDescriptor As New GroupDescriptor()
countryDescriptor.Member = "Country"
countryDescriptor.SortDirection = ListSortDirection.Ascending
countryDescriptor.AggregateFunctions.Add(f)
countryDescriptor.AggregateFunctions.Add(f1)
Me.radGridView.GroupDescriptors.Add(countryDescriptor)

The final result can be seen on the snapshot below:

Figure 1: RadGridView with GroupDescriptor and AggregateFunctions defined

With Q3 2012 we added built-in feature for aligning header aggregates with corresponding columns.

Align Header Aggregates

Figure 2: Aligning the Aggregates

Rad Grid View Group Aggregates 3

This feature can be controlled with applying a Style. The Style should be targeting at the GroupHeaderRow visual element having the GroupRenderMode of the RadGridView set to Flat.
Here is an example of the style definition:

Example 5: Settng the ShowGroupHeaderColumnAggregates property of GroupHeaderRow

<Style TargetType="telerik:GroupHeaderRow">
    <Setter Property="ShowGroupHeaderColumnAggregates" Value="True" />
</Style>

When using this feature, most probably you don't need the group row aggregates to be shown any more. You can hide them extending the above style as follows:

Example 6: Setting the ShowHeaderAggregates property of GroupHeaderRow to False

<Style TargetType="telerik:GroupHeaderRow">
    <Setter Property="ShowGroupHeaderColumnAggregates" Value="True" />
<Setter Property="ShowHeaderAggregates" Value="False" />  

As of Q3 2012 we have introduced a new rendering mode of RadGridView - Flat. The default GroupRenderMode is Nested, and the new one is Flat. When you set the Flat mode, the GridView will render rows one below the other. This leads to a very good perfromance when the grid is grouped on several levels and has a lot of data. You can also refer to the Grouping Modes article.

Setting the "ShowHeaderAggregates" to false will hide the default header aggregates.

Figure 3: RadGridView with ShowHeaderAggregates set to False

Rad Grid View Group Aggregates 4

For more details you can check our Aggregates demo.

You can download a runnable project on how to sort a group by aggregate from our online SDK repository here, the example is listed as GridView/SortGroupByAggregate .

You can also check the SDK Samples Browser that provides a more convenient approach in exploring and executing the examples in the Telerik XAML SDK repository.

Align Column Header Aggregates in GroupHeaderRow

When using the ShowGroupHeaderColumnAggregates property of GroupHeaderRow, the aggregates will be aligned in the GroupFooterRow element. If aligning them in the GroupHeaderRow is required, you need to use the ColumnAggregatesAlignment property of RadGridView instead. It is an enumeration and provides three values:

  • BelowGroupKey

  • NextToGroupKey

  • NoAlignment

Figure 4: RadGridView with ColumnAggregatesAlignment set to BelowGroupKey

Rad Grid View Group Aggregates 4

Figure 5: RadGridView with ColumnAggregatesAlignment set to NextToGroupKey

Rad Grid View Group Aggregates 4

Figure 6: RadGridView with ColumnAggregatesAlignment set to NextToGroupKey

Rad Grid View Group Aggregates 4

See Also