Edit this page

This article will explain some more information on:

Aggregate Functions

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

The available functions are:

  • 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 elements 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.

The footer cell displays caption and result for selected aggregate function.

To display an aggregate function for a certain column, just instantiate the function you need and add it to the AggregateFunctions collection of that column.

[XAML] 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>

[C#] Example 2: Add an aggregate function for a certain column

CountFunction f = new CountFunction();
((GridViewDataColumn)this.radGridView.Columns["ID"]).AggregateFunctions.Add(f);

[VB.NET] Example 2: Add an aggregate function for a certain column

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

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

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

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

We use System.Linq to generate a proper Expression to do the calculation based on aggregate function defined and type of the bound data.

Autogenerated Columns

You can subscribe for AutoGeneratingColumn event and additionally configure a column.
For example:

[C#] Example 3: Configuring column on AutoGeneratingColumn event raised

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 DataMemberBinding for the column.

Update Results

If a recalculation of aggregates is needed, you may call CalculateAggregates() method of the grid:

[C#] Example 4: Invoke CalculateAggregates() method

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

That way column aggregates will be updated and group aggregates will not be. Please check this troubleshooting article on how to refresh them.

Modify Displayed Aggregates

You can modify the default template of displaying aggregates by overriding GridViewColumn.Footer configured it per specific requirements.
For example:

[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 AggregateResultsList.

Group Aggregates

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

See Also