Aggregate Functions

Column footers can be used to display aggregated information about the data in the column by defining aggregate functions. The aggregate functions are included in Telerik.Windows.Data assembly.

Here's a list of the available functions:

  • AverageFunction: Returns the average of the values in the column.

  • CountFunction: Returns the number of all items in the column.

  • FirstFunction: Returns the first element in the column according to the current sorting.

  • LastFunction: Returns the last element in the column according to the current sorting.

  • MaxFunction: Returns the maximum value of the cell values in the column.

  • MinFunction: Returns the minimum value of the cell values in the column.

  • SumFunction: Returns the sum of all cell values in the column.

You can also create your own custom functions similar to these by inheriting the Telerik.Windows.Data.EnumerableAggregateFunction class.

You could also use a Generic AggregateFunction to define some custom aggregate expressions. An example can be found here.

If the ShowColumnFooters property of the RadGridView control is set to True, the footer cells will display a caption and the result for the defined aggregate functions.

Figure 1: RadGridView with aggregate results

Telerik Silverlight DataGrid Aggregate Functions

To specify an aggregate function for a certain column, just add it to the AggregateFunctions collection of that column.

Example 1: Display an aggregate function for a certain column

<telerik:GridViewDataColumn Header="ID" 
                DataMemberBinding="{Binding EmployeeID}" 
        <telerik:CountFunction Caption="Count: " /> 

Example 2: Add an aggregate function for a certain column

CountFunction f = new CountFunction(); 
Dim f As New CountFunction() 
DirectCast(Me.radGridView.Columns("ID"), GridViewDataColumn).AggregateFunctions.Add(f) 

Figure 2: RadGridView with a defined CountFunction

Telerik Silverlight DataGrid Aggregate Functions 010

You need to set RadGridView's ShowColumnFooters property of to true in order to display aggregates.

You need to set RadGridView's ShowGroupFooters property of RadGridView to true in order to display aggregates under Grouped rows.

RadGridView uses System.Linq to generate a proper Expression to do the calculation based on aggregate function defined and type of the bound data. Note that if an expression cannot be automatically generated, an exception will be thrown.

Autogenerated Columns

You can subscribe to the AutoGeneratingColumn event and additionally configure a column as demonstrated in Example 3.

Example 3: Configuring column on AutoGeneratingColumn

private void clubsGrid_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e) 
    if (e.Column.UniqueName == "Name") 
        e.Column.AggregateFunctions.Add(new CountFunction()); 

If you do not explicitly specify a UniqueName, then its value will be the name of the property set as the DataMemberBinding for the column.


You can specify a format string for the aggregate results by setting the ResultFormatString property of an aggregate function.

Example 4: Specifying ResultFormatString

    <telerik:SumFunction Caption="Total: $" ResultFormatString="{}{0:C2}"/> 
The code from Example 4 will display the aggregate results in the currency format with two digits after the decimal separator.

Update Results

If you need to recalculate the aggregate results, you can call the CalculateAggregates method of the control as shown in Example 4.

Example 5: Invoke CalculateAggregates() method

private void Button1_Click(object sender, RoutedEventArgs e) 

Note that group aggregates will not be updated using this approach. Please check this troubleshooting article on how to refresh them.

Modify Displayed Aggregates

You can modify the default template of the displayed aggregates by setting the Footer property of the column as per your specific requirements.

Example 6: Overriding GridViewDataColumn.Footer

    <StackPanel Orientation="Vertical" 
        <TextBlock Text="Custom footer with aggregates:" 
                   Margin="0,0,0,2" /> 
        <telerik:AggregateResultsList ItemsSource="{Binding}" 
                    <StackPanel Orientation="Horizontal" 
                        <TextBlock VerticalAlignment="Center" 
                                   Text="{Binding Caption}" /> 
                        <TextBlock VerticalAlignment="Center" 
                                   Text="{Binding FormattedValue}" /> 
                    <StackPanel Orientation="Vertical" /> 

Note that the Footer's DataContext is of type AggregateResultsList.

Group Aggregates

Aggregate functions can also be defined per group. To find out more take a look at the Group Aggregates article.

Default AggregateFunctions for a Custom Column

If you need to have a default aggregate function for the custom column which is not added in XAML, you need to add it after the initialization of the column. This can be done by overriding the EndInit method of FrameworkContentelement.

Example 7: Adding a default AggregateFunction

public class MyColumn : GridViewDataColumn 
    public MyColumn() 
    public override void EndInit() 
        this.AggregateFunctions.Add(new SumFunction()); 

See Also

In this article
Not finding the help you need? Improve this article