我最终解决了这个问题,并将我的解决方案张贴在这里,以防其他人发现它有用。
如果加权平均值由每行的值和权重组成,则包含该值的列应将weight gridcolumn对象分配给其tag属性。然后,此事件处理程序将执行其余操作:
private static void gridView_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e)
{
GridColumn weightColumn = ((GridSummaryItem)e.Item).Tag as GridColumn;
if (weightColumn == null)
return;
switch (e.SummaryProcess)
{
case CustomSummaryProcess.Start:
{
e.TotalValue = new WeightedAverageCalculator();
break;
}
case CustomSummaryProcess.Calculate:
{
double size = Convert.ToDouble(e.FieldValue);
double weight = Convert.ToDouble(((GridView)sender).GetRowCellValue(e.RowHandle, weightColumn));
((WeightedAverageCalculator)e.TotalValue).Add(weight, size);
break;
}
case CustomSummaryProcess.Finalize:
{
e.TotalValue = ((WeightedAverageCalculator)e.TotalValue).Value;
break;
}
}
}
private sealed class WeightedAverageCalculator
{
private double _sumOfProducts;
private double _totalWeight;
public void Add(double weight, double size)
{
_sumOfProducts += weight * size;
_totalWeight += weight;
}
public double Value
{
get { return _totalWeight==0 ? 0 : _sumOfProducts / _totalWeight; }
}
}
代码假定基础列值可以通过
Convert.ToDouble(object)
.