TotalSummary of GroupSummaries in Devexpress

Soner Gönül picture Soner Gönül · May 10, 2011 · Viewed 8.1k times · Source

I have an ASPxGridview like this:

enter image description here

Is there any way calculate Total of GroupSummary to TotalSummary without Grouping.

GroupSummary's  SummeryType="AVERAGE"

For Example:

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

Then I want TotalSummary of RISK_EUR column is 123.456 + 234.567 = 358023.

NOTE: I only want this calculation with normal Gridview. Not doing with Grouping.

Another example:

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

I want with that grid:

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

And then TotalSummary of Price is = 70 + 60 + 30 = 160

How can I do that? Or what is this code about? Which function should I use?

Answer

DevExpress Team picture DevExpress Team · May 10, 2011

I see two different solutions:

1) implement the data management manually:
a) sort data by the pseudo group column; b) browse through the sorted data list, calculate the summary values manually and finally show this value;

2) create a new grid on the page, bind it with data, group by the required column, fetch the summary values and finally dispose it.

I did not check the second approach, but I do not see a reason on why this approach should not work.

UPDATE

It is only possible to set a summary value if you are using custom summary. This can be done within the CustomSummaryCalculate event handler. Also to obtain summary values, you can use the following code:

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

You should implement something like this.

Update 2 OK, I think I have found the most effective solution to this problem. Let me explain. First, it is necessary to use a custom summary as it is explained in the Custom Summary topic. Using the CustomSummaryCalculate event handler, it is necessary to collect data to a Dictionary object, whose key contains the Customer_No field value, value - list of Price values for this Customer. Finally, it is necessary to calculate the resulting summary. Below is the complete code, both ASPx and C#. I hope, it will be helpful to you.

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