gpt4 book ai didi

.net - 如何向 DevExpress XtraGrid 添加加权平均摘要?

转载 作者:行者123 更新时间:2023-12-04 07:09:43 27 4
gpt4 key购买 nike

DevExpress Grid (XtraGrid) 允许网格及其组进行汇总计算。可用选项包括计数、最大值、最小值、平均值、总和、无和自定义。

有没有人得到一些示例代码,显示如何根据另一列中作为值提供的权重来计算加权平均列?

最佳答案

我最终解决了这个问题,并将在此处发布我的解决方案,以防其他人发现它有用。

如果加权平均值同时包含一个值和每行的权重,则包含该值的列应将权重 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) 转换为 double 值。 .

关于.net - 如何向 DevExpress XtraGrid 添加加权平均摘要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/569162/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com