【问题标题】:TotalSummary of GroupSummaries in DevexpressDevexpress 中 GroupSummaries 的 TotalSummary
【发布时间】:2011-05-10 19:57:18
【问题描述】:

我有一个这样的ASPxGridview

有什么方法可以计算GroupSummaryTotalSummary 的总数不分组

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

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

【问题讨论】:

    标签: c# .net asp.net devexpress aspxgridview


    【解决方案1】:

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

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

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

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

    更新

    只有在使用自定义摘要时才能设置摘要值。这可以在 CustomSummaryCalculate 事件处理程序中完成。另外要获取汇总值,可以使用以下代码:

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

    你应该实现这样的东西。

    更新 2 好的,我想我已经找到了解决这个问题的最有效的方法。让我解释。首先,有必要使用自定义摘要,如 Custom Summary 主题中所述。使用 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;
        }
    

    【讨论】:

    • 但我怎么能这样做; if(grid.consist) 然后合计所有 GroupSummary["IPOTEK"] 并分配给 TotalSummary["IPOTEK"]。你能举个这样的例子吗?
    • @Dev 谢谢!这段代码计算groupsummary的总数而不分组对吗?
    • @Soner Gönül,此代码用于您应该创建的假网格。您还应该将其绑定到数据。它应该包含一个分组列和分组汇总项。在这种情况下,您可以浏览其行,获取分组汇总值,然后设置自定义汇总项总值...
    • @Devexpress 我试试你的代码heypasteit.com/clip/XZI 但还是不行。我仍然需要你的帮助!
    • @Dev 我已经有一个 SqlDataSource 并与 ASPxGridview 连接。我应该在我的代码中删除 CreateDataSource()GetDataSource() 吗?
    【解决方案2】:

    让你的 sql 返回值:

    select (select SUM(x) from foo f2 where f1.x = f2.x) as sum, f1.x from foo f1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多