Group and Footer Aggregates using Multiple Columns in Calculation

4 posts, 1 answers
  1. NR
    NR avatar
    17 posts
    Member since:
    Oct 2009

    Posted 16 Mar 2011 Link to this post

    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?
  2. Answer
    Maya
    Admin
    Maya avatar
    4062 posts

    Posted 18 Mar 2011 Link to this post

    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
  3. NR
    NR avatar
    17 posts
    Member since:
    Oct 2009

    Posted 25 Mar 2011 Link to this post

    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.
  4. Rudi
    Rudi avatar
    1 posts
    Member since:
    May 2011

    Posted 16 Nov 2011 Link to this post

    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.
Back to Top