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

Calculate value based on aggregate result value.

6 Answers 266 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Pete
Top achievements
Rank 1
Pete asked on 06 Jun 2011, 12:16 AM
My column footers are summed using an aggregate (SumFunction).
I need to calculate the percent of a grand total represented by the sum of each column. The grand total is calculated as a sum function too.

Essentially I'm looking to do the following: (think Excel)

B5/E5  C5/E5  D5/E5 etc...where E5 is the grand total in the totals column and B5, C5 etc are the sums for their respective columns.

I'm following the pattern for creating a custom aggregate. Here is what I have so far.  What I need is a way to pass the grand total value to the Percent method. Or if that isn't possible some other approach?

public static double Percent<TSource>(IEnumerable<TSource> source, Func<TSource, int?> selector)
{
  return (source.Select(selector).Aggregate(0, (t1, t2) => t1 + (int)t2) * .01);
}

6 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 08 Jun 2011, 03:14 PM
Hi Peter,

In order to provide you with a more accurate solution, I would need a bit more details on your exact requirements. Where do you want to display that value - do you want it to be in a separate column or in the footers of each column ? It would be great if you could share a simple example of how you want your grid to look like.
  

Kind regards,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Pete
Top achievements
Rank 1
answered on 09 Jun 2011, 11:24 AM
Thank you for your reply Maya.
Here is what I'm looking for.

        Col1    Col2    Totals
        10      90      100
        15      85      100
        20      80      100
        40      60      100
Total       85     315      400
Percent  21.25%  78.75%  100.00%


Thanks
0
Maya
Telerik team
answered on 10 Jun 2011, 01:20 PM
Hi Peter,

I am sending you a sample project illustrating a possible approach for calculating the sum of all aggregates and defining a new aggregate function for a specific column.
Let me know whether it corresponds to your requirements.

Regards,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Pete
Top achievements
Rank 1
answered on 13 Jun 2011, 02:42 PM
Thanks Maya. I'll try it out and get back to you!
0
Pete
Top achievements
Rank 1
answered on 19 Jun 2011, 11:05 PM
Hi Maya;

Thanks for your efforts, however, I'm still in need of a solution.  

Interestingly enough if I define the following using a column I know to exist the aggregation works to sum the column.

var totalColumnAggregate = new AggregateFunction<DataRow, int?>
{
    AggregationExpression = target => target
        .Select(c => c.Field<int?>("Surv"))
        .Sum()
};

But, if I use Total, as in the below example, I just get 0, even though I have values in my Total column. Total column is of type GridViewExpressionColumn and its values come from the evaluation of its Expression property. What could the reason for this behavior where "Surv" sums correctly but "Total" does not?

var totalColumnAggregate = new AggregateFunction<DataRow, int?>
{
    AggregationExpression = target => target
        .Select(c => c.Field<int?>("Total"))
        .Sum()
};
0
Maya
Telerik team
answered on 20 Jun 2011, 09:30 AM
Hi Peter,

Please take a look at this forum thread for a reference on how to define an aggregate function for the GridViewExpressionColumn.
 

Greetings,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Pete
Top achievements
Rank 1
Answers by
Maya
Telerik team
Pete
Top achievements
Rank 1
Share this question
or