6

我有一个ASPxGridview这样的:

在此处输入图像描述

有没有办法计算 Total of GroupSummaryto TotalSummary without Grouping

GroupSummary's  SummeryType="AVERAGE"

例如:

MUS_K_ISIM   GroupSummary[RISK_EUR]
2M LOJİSTİK  123.456 
ABA LOJİSTIK 234.567 

然后我想要TotalSummaryRISK_EUR列是 123.456 + 234.567 = 358023

注意:我只希望使用普通 Gridview 进行此计算。不做分组。

另一个例子:

Customer_No Customer_Name Price
123         aaa           50
123         aaa           100
123         aaa           60
124         bbb           60
125         ccc           20
125         ccc           40

我想要那个网格:

What is avarage of 123 number customer = 50 + 100 + 60 = 210/3= 70
What is avarage of 124 number customer = 60/1=60
What is avarage of 125 number customer = 20 + 40 = 60/2= 30

然后 TotalSummary of Price 是 = 70 + 60 + 30 = 160

我怎样才能做到这一点?或者这段代码是关于什么的?我应该使用哪个功能?

4

2 回答 2

3

我看到两种不同的解决方案:

1) 手动实现数据管理:
a) 按伪分组列对数据进行排序;b) 浏览排序后的数据列表,手动计算汇总值,最后显示该值;

2)在页面上创建一个新的网格,将其与数据绑定,按所需列分组,获取汇总值并最终处理它。

我没有检查第二种方法,但我看不出为什么这种方法不起作用的原因。

更新

如果您使用自定义摘要,则只能设置摘要值。这可以在 CustomSummaryCalculate 事件处理程序中完成。同样要获取汇总值,您可以使用以下代码:

double total = 0;
                for(int i = 0; i < ASPxGridView1.VisibleRowCount; i ++) {
                    total += Convert.ToDouble(ASPxGridView1.GetGroupSummaryValue(i, someSummaryItem));
                }

你应该实现这样的东西。

更新 2 好的,我想我已经找到了解决这个问题的最有效的方法。让我解释。首先,有必要使用自定义摘要,如自定义摘要主题中所述。使用 CustomSummaryCalculate 事件处理程序,有必要将数据收集到 Dictionary 对象,其键包含 Customer_No 字段值,value - 此客户的价格值列表。最后,有必要计算得出的摘要。下面是完整的代码,包括 ASPx 和 C#。我希望,它会对你有所帮助。

    <dx:ASPxGridView ID="ASPxGridView1" runat="server" OnCustomSummaryCalculate="ASPxGridView1_CustomSummaryCalculate">
        <TotalSummary>
            <dx:ASPxSummaryItem FieldName="Price" SummaryType="Custom" ShowInColumn="Price" />
        </TotalSummary>
        <Settings ShowFooter="True" />
    </dx:ASPxGridView>

...

using System;
using System.Collections.Generic;
using System.Data;
using System.Collections;

    protected void Page_Init(object sender, EventArgs e) {
        ASPxGridView1.DataSource = GetDataSource();
        ASPxGridView1.DataBind();
    }

    private object CreateDataSource() {
        DataTable table = new DataTable();
        table.Columns.Add("Customer_No", typeof(int));
        table.Columns.Add("Price", typeof(int));
        table.Rows.Add(new object[] {123, 50 });
        table.Rows.Add(new object[] {123, 100 });
        table.Rows.Add(new object[] {123, 60 });
        table.Rows.Add(new object[] {124, 60 }); 
        table.Rows.Add(new object[] {125, 20 });
        table.Rows.Add(new object[] {125, 40 });
        return table;
    }
    private object GetDataSource() {
        if(Session["data"] == null)
            Session["data"] = CreateDataSource();
        return Session["data"];
    }

    Dictionary<int, List<int>> dict;
    protected void ASPxGridView1_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e) {
        if(e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Start)
            dict = new Dictionary<int, List<int>>();
        if(e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Calculate) {
            int customer_No = Convert.ToInt32(e.GetValue("Customer_No"));
            List<int> list;
            if(!dict.TryGetValue(customer_No, out list)) {
                list = new List<int>();
                dict.Add(customer_No, list);
            }
            list.Add(Convert.ToInt32(e.GetValue("Price")));
        }
        if(e.SummaryProcess == DevExpress.Data.CustomSummaryProcess.Finalize) {
            e.TotalValue = CalculateTotal();
        }
    }
    private object CalculateTotal() {
        IEnumerator en = dict.GetEnumerator();
        en.Reset();
        float result = 0;
        while(en.MoveNext()) {
            KeyValuePair<int, List<int>> current = ((KeyValuePair<int, List<int>>)en.Current);
            int sum = 0;
            for(int i = 0; i < current.Value.Count; i++)
                sum += current.Value[i];
            result += sum / current.Value.Count;
        }
        return result;
    }
于 2011-05-10T21:44:09.827 回答
0

让你的 sql 返回值:

select (select SUM(x) from foo f2 where f1.x = f2.x) as sum, f1.x from foo f1
于 2011-05-20T05:21:01.673 回答