2011-05-10 21 views
6

我有一個ASPxGridview這樣的:TotalSummary在DevExpress的

enter image description here

是否存在的GroupSummaryTotalSummary任何方式計算總不進行分組

GroupSummary's SummeryType="AVERAGE" 

例如:

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

然後我想RISK_EUR柱的TotalSummary爲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是= 70 + 60 + 30 = 160

我怎麼能這樣做? 或者這段代碼是關於什麼的?我應該使用哪個功能?

回答

3

我看到兩種不同的解決方案:

1)手動執行數據管理:
一個)由僞組列排序的數據; b)瀏覽已排序的數據列表,手動計算彙總值並最終顯示該值;

2)在頁面上創建一個新的網格,將其與數據綁定,按所需列進行分組,獲取彙總值並最終處理它。

我沒有檢查第二種方法,但我沒有看到爲什麼這種方法不適用的原因。

UPDATE

這是唯一可能的,如果你使用的是自定義摘要設置彙總值。這可以在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的價值值Customer_No字段值。最後,有必要計算結果摘要。以下是完整的代碼,包括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; 
    } 
+0

但我怎麼能是這樣做的; if(grid.consist)然後總計所有GroupSummary [「IPOTEK」]並分配給TotalSummary [「IPOTEK」]。你能舉出這樣的例子嗎? – 2011-05-11 08:31:14

+0

我已更新我的回答Soner – 2011-05-11 19:16:07

+0

@Dev謝謝!這段代碼計算沒有分組權利的組總數? – 2011-05-11 19:26:50

0

具有SQL返回值:

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