Edit this page

Aggregate Functions

This article will discuss the following topics:

Types of 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

RadGridView with aggregate results

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}"
                UniqueName="ID">
    <telerik:GridViewDataColumn.AggregateFunctions>
        <telerik:CountFunction Caption="Count: " />
    </telerik:GridViewDataColumn.AggregateFunctions>
</telerik:GridViewDataColumn>

Example 2: Add an aggregate function for a certain column

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

Figure 2: RadGridView with a defined CountFunction

RadGridView with a defined CountFunction

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.

You need to set RadGridView's SourceField property of AggregateFunctions collection to specify a field to use in calculating the result.

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.

[C#] 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.

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 4: Invoke CalculateAggregates() method

private void Button1_Click(object sender, RoutedEventArgs e)
{
   this.radGridView.CalculateAggregates();
}

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.

[C#] Example 5: Overriding GridViewDataColumn.Footer

<telerik:GridViewDataColumn.Footer>
    <StackPanel Orientation="Vertical"
                Margin="0,10">
        <TextBlock Text="Custom footer with aggregates:"
                   Margin="0,0,0,2" />
        <telerik:AggregateResultsList ItemsSource="{Binding}"
                                      VerticalAlignment="Center"
                                      Grid.Column="4">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal"
                                VerticalAlignment="Center">
                        <TextBlock VerticalAlignment="Center"
                                   Text="{Binding Caption}" />
                        <TextBlock VerticalAlignment="Center"
                                   Text="{Binding FormattedValue}" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </telerik:AggregateResultsList>
    </StackPanel>
</telerik:GridViewDataColumn.Footer>

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.

See Also