I have a GridViewExpressionColumn and I want to calculate the sum of the elements of the column.
I think I have to use a Generic AggregateFunction but I can't figure out how to use it, and I can't find any example. Can anyone provide a simple example on how to use Generic AggregateFunction for a GridViewExpressionColumn ?
Thank you.
9 Answers, 1 is accepted
I am sending you a sample project illustrating how you may benefit from the Generic AggregateFunction. Let me know if you need any further assistance.
Maya
the Telerik team
Thanks for your help.
You may extend the provided example by setting the aggregate expression as an aggregate function for a GridViewExpressionColumn. I am sending you the updated sample.
Maya
the Telerik team
Here's what I wrote
<telerik:GridViewExpressionColumn Header="Calculated field" UniqueName="Calculated" > <telerik:GridViewExpressionColumn.AggregateFunctions> <telerik:SumFunction Caption="Total: " /> </telerik:GridViewExpressionColumn.AggregateFunctions></telerik:GridViewExpressionColumn>Thanks in advance
For the time being the only way for defining aggregates for the GridViewExpressionColumn is to follow the approach demonstrated in the sample project - to create a generic AggregateFunction and add it in the AggregateFunctions collection of that column.
Maya
the Telerik team
There's an example of generic AggregateFunction but it uses the SourceField that doesn't exist for a GridViewExpressionColumn. And I can't see any way to access the values of the GridViewExpressionColumn. Can't you provide sample code that show how to calculate the sum of the "Total value in stock" of the "Calculated column" (RadGridView) example ?
Thanks in advance.
May you take another look on the last sample project I attached in this thread ? Correct me if I am wrong, but the following lines define an aggregate function on a GridViewExpressionColumn and that function returns the sum of the values in that column:
Expression<Func<Player, int>> expression = p => p.Number - p.Number1;var column = this.playersGrid.Columns["MyExpressionColumn"] as GridViewExpressionColumn;column.Expression = expression;var aggregate = new AggregateFunction<Player, int>{ AggregationExpression = players => players.Sum(p => p.Number - p.Number1), Caption = "Sum:"};column.AggregateFunctions.Add(aggregate);Let me know in case there is some misunderstanding.
Maya
the Telerik team
Just one thing remaining, if I modify a record in my ViewModel the grid is updated accordingly, but not the result of the AggregateFunction. How can I force the update without making a whole update ?
Thanks again.
A possible approach may be to call CalculateAggregates() method of the RadGridView.
Maya
the Telerik team