How do I add a summary that calculates a weighted average and handles when the group by columns gets changed? Do I need to create a custom summary? If so, are there examples of doing this? I have not ventured into creating my own summaries yet. thanks
I'm not exactly sure how a weighted average is calculated. I assume this would depend on two columns of the grid instead of just one, though.
If that's the case, then you would have to calculate this yourself using a custom summary. It's pretty simple. Create a class that implement ICustomSummaryCalculator and assign an instance of that class to the summary when you create it.
There are three methods on this interface, one that begins the calculation, one that ends it, and one that fires passing in the rows for which the summary is being calculated.
hey Mike, i'm trying to accomplish this exact task but having a bit of trouble. i am doing a weighted avg calc and i think i'm off in scope of what the custom class is doing. this is my weighted avg calc class. the field i'm avging is called price and its being weighted by cumqty.
daverage is not coming out as my weighted average. what am i missing? this class is called for every grouping/calculation right?
any help is appreciated.
Al
public class WeightAvgSummary:ICustomSummaryCalculator { decimal daverage = 0; decimal dweight = 0; decimal denominator = 0; public WeightAvgSummary () { }
//get total TOTALCUMQTY for denominator public void BeginCustomSummary (SummarySettings settings,RowsCollection rows) {
denominator = (from r in rows where r.IsDataRow == true select Convert.ToDecimal(r.Cells["CUMQTY"].Value)).Sum();
if (denominator == 0) denominator = 1; }
//per row get the weight of this row CUMQTY/TOTALCUMQTY gives me the weight for that row. and i sum it with a running total...(daverage).
//multiply that weight * PRICE public void AggregateCustomSummary(SummarySettings settings, UltraGridRow row) { dweight = Convert.ToDecimal(row.Cells["CUMQTY"].Value) / denominator; daverage += (dweight/100 * Convert.ToDecimal(row.Cells["PRICE"].Value)); } public object EndCustomSummary(SummarySettings settings, RowsCollection rows) { return daverage; } }