This is a migrated thread and some comments may be shown as answers.

Group and Footer Aggregates using Multiple Columns in Calculation

3 Answers 140 Views
GridView
This is a migrated thread and some comments may be shown as answers.
NR
Top achievements
Rank 1
NR asked on 16 Mar 2011, 11:27 PM
I haven't seen a thread on this is a while, so I wanted to check again and see where the status of this is.

I have a RadGridView control currently that I need to have more functionality on. The grid contains a lot of data (much of it pre-calculated on the DB side before coming into the application). The problem I have however is that a couple of the columns are averages within the data, or use multiple columns to calculate the value using something like "SUM(col1) / SUM(col2)". 

I know that you can extend the CustomAggregates and create a new aggregate function, but as far as I can tell, this only works on the column itself being calculated, and cannot pull in the data from another column in the calculation.

This becomes a problem because I cannot perform an AVG() on a column that already contains averages, because then I'm just averaging the average and it is not the same result as calculating based on the other columns. This is also a problem in the case where the calculation that is supposed to display is the result of SUM(col1) / SUM(col2). 

Is there any way currently within the RadGridView to perform this calculation at the Footer level and Grouping level?

3 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 18 Mar 2011, 09:36 AM
Hi NR,

You may use a Generic AggregateExpression. So, let us say we have an object of type Player that has two properties Number1 and Number2.  The expression may be as follows:

var gmAggregateFunction = new AggregateFunction<Player, double>
            {
                AggregationExpression = player => (double)player.Sum(c => c.Number1) / (double)player.Sum(c => c.Number),
                Caption = "AggExpression: "
  
            };
            this.playersGrid.Columns["Number"].AggregateFunctions.Add(gmAggregateFunction);

You may add the same in the AggregateFunctions collection of a group descriptor as well.

 

Kind regards,
Maya
the Telerik team
0
NR
Top achievements
Rank 1
answered on 25 Mar 2011, 09:28 PM
This is a GREAT addition to the Telerik GridViews. I had to declare a different function to serve as the computation function since it was possible that SUM(column) could return 0 in the divisor, and in those cases I wanted to return 0 as the final value. I simply added a function like this:
private static double SafeDivideReturnZero(double top, double bottom) { return (bottom != 0) ?  top / bottom : 0; }

Then I added the grid column aggregate like so:
rgvDetails.Columns["calcMetric"].AggregateFunctions.Add(
    new AggregateFunction<DetailMetric, double>
    {
        AggregationExpression = detail => SafeDivideReturnZero((double)detail.Sum(m => m.value1), (double)detail.Sum(m => m.value2)),
        Caption = "Calc:",
        ResultFormatString = "{0:C2}",
    }
);

It worked exactly like I needed it to. Thanks again for your assistance.
0
Rudi
Top achievements
Rank 1
answered on 16 Nov 2011, 09:42 AM
Hi,

I saw this example of customer  aggreagetFunctionthis is exactly what I need
 but I have a problem that the itemSource in my grid is denumic and i dont know the type of the itemSource before like
DetailMetric type in the example
My application is like:
I have  a DataTable class that have a property of List<DataColumn> Columns
the itemSource is of DataTable and i add the columns for the itemsource denumic

so how can i do it?

Thank you so mutch!
Rudi.
Tags
GridView
Asked by
NR
Top achievements
Rank 1
Answers by
Maya
Telerik team
NR
Top achievements
Rank 1
Rudi
Top achievements
Rank 1
Share this question
or