如何将加权平均摘要添加到 DevExpress XtraGrid?

发布于 2024-07-13 21:07:06 字数 127 浏览 6 评论 0原文

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

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

The DevExpress Grid (XtraGrid) allows grids and their groups to have summary calculations. The available options are Count, Max, Min, Avg, Sum, None and Custom.

Has anyone got some sample code that shows how to calculate a weighted average column, based upon the weightings provided as values in another column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒时光 2024-07-20 21:07:06

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

如果加权平均值由每行的值和权重组成,则包含该值的列应将权重 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) 转换为双精度值。

I ended up working this out, and will post my solution here in case others find it useful.

If a weighted average consists of both a value and a weight per row, then column that contains the value should have the weight GridColumn object assigned to its Tag property. Then, this event handler will do the rest:

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; }
    }
}

The code assumes that the underlying column values can be converted to doubles via Convert.ToDouble(object).

¢蛋碎的人ぎ生 2024-07-20 21:07:06

如果您在总和中提供自己的 lambda 表达式,您应该能够按标准总和对它们进行分组。 我认为这应该有效:

var grouped = from item in items
orderby item.Group
group item by item.Group into grp
select new
{
Average= grp.Sum(row => row.Value * row.Weight)
};

If you provide your own lambda expression inside the sum you should be able to group them by a standard sum. I think this should work:

var grouped = from item in items
orderby item.Group
group item by item.Group into grp
select new
{
Average= grp.Sum(row => row.Value * row.Weight)
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文