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.
Tip |
|---|
You can also create your own custom functions similar to these by inheriting the 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.
CopyXAML
<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>
CopyC#
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 );
CopyVB.NET
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 the AggregateFunctions collection and it will be visualized after the first one.
Note |
|---|
The FirstFunction will display the value returned by the ToString() method of your business object. |
CopyXAML
<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>
CopyC#
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 );
CopyVB.NET
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:
With Q3 2012 we added built-in feature for aligning header aggregates with corresponding columns.
Align Header Aggregates
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:
CopyXAML
<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:
CopyXAML
<Style TargetType="telerik:GroupHeaderRow">
<Setter Property="ShowGroupHeaderColumnAggregates" Value="True" />
<Setter Property="ShowHeaderAggregates" Value="False" />
</Style>
Setting the "ShowHeaderAggregates" to false will hide the default header aggregates.
For more details you can check our
Aggregates demo.
See Also