This is a migrated thread and some comments may be shown as answers.

Custom Gridview summary

2 Answers 283 Views
GridView
This is a migrated thread and some comments may be shown as answers.
todd
Top achievements
Rank 1
todd asked on 25 Jan 2013, 04:46 PM
Hello,
I have a RadGridView and I want to show summary of the column with some condition.
The data is as follow
____________________
name | price | discount
---------------------------------
name1 | price2 | discount2
---------------------------------
name2 | price3 | discount3

---------------------------------
name2 | price4 | discount4

---------------------------------

I want to my gird show summary something like this:
----------------------------------
total price of name1: price2  * discount2
total price of name2: (price3 * discount3) + (price4 + discount4)
----------------------------------
Grouping the name of name column to set the name of row summary, here is name1 and name2. In other hand, get the price and discount where name column to calculate total price. How can I customize this sum operation?


2 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Todorov
Telerik team
answered on 30 Jan 2013, 09:07 AM
Hi Todd,

Thank you for contacting us.

You can use the GroupSummaryEvaluate event to add a custom implementation of the summary evaluation:
public Form1()
{
    InitializeComponent();
    Random rand = new Random();
    for (int i = 0; i < 10; i++)
    {
        this.radGridView1.Rows.Add("Name " + i, rand.NextDouble() * 100, 0.8 + rand.NextDouble() * 0.1);
    }
 
 
    GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
     
    GridViewSummaryItem summaryItem = new GridViewSummaryItem();
    summaryItem.Name = "Price";
    summaryItem.Aggregate = GridAggregateFunction.Sum;
    summaryRowItem.Add(summaryItem);
     
    this.radGridView1.SummaryRowsBottom.Add(summaryRowItem);
 
    this.radGridView1.GroupSummaryEvaluate += new GroupSummaryEvaluateEventHandler(radGridView1_GroupSummaryEvaluate);
}
 
void radGridView1_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
{
    decimal value = 0;
    if (e.SummaryItem.Name == "Price")
    {
        foreach (GridViewRowInfo row in this.radGridView1.Rows)
        {
            value += ((decimal)row.Cells["Price"].Value) * ((decimal)row.Cells["Discount"].Value);
        }
    }
 
    e.Value = value;
}

I hope you find this useful. If you have any additional questions, do not hesitate to ask.

Regards,
Ivan Todorov
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
özer
Top achievements
Rank 2
Veteran
Iron
commented on 11 May 2023, 08:30 PM

Hi.

I tryed this solution and two  major errors occured.

If I don't group by any column, it's fine.

But if i group it by any column

1- The name of the grouped column appears as "0"
2- The sums of the grouped rows appear to be incorrect. The total of all rows is displayed instead of the sum of the grouped rows.

This is my code


private void rgvSgkOtomasyon_GroupSummaryEvaluate(object sender, GroupSummaryEvaluationEventArgs e)
        {
            decimal value = 0;
            foreach (GridViewRowInfo row in this.rgvSgkOtomasyon.Rows)
            {
                // If the value of the column named "bm" in the row is "Ä°PTAL", do not include the values in this row in the total. 
               // Or if the FormatString value of the column does not contain "{0:C}" (i.e. non-numeric), do not add on that column.
                if (row.Cells["bm"].Value.ToString() == "Ä°PTAL" || !e.SummaryItem.FormatString.Contains("0:C")) continue;
                value += (decimal)row.Cells[e.SummaryItem.Name].Value;
            }

            e.Value = value;
        }
Thank you in advance for your help.
Dess | Tech Support Engineer, Principal
Telerik team
commented on 12 May 2023, 04:34 AM

Hi, Ã¶zer,

Please have in mind that RadGridView exposes two collections that contain data rows:

Rows - contains all data rows that belong to RadGridView. Data operations such as grouping, sorting, filtering, etc. do not change the content of the collection or the order in which the row objects exist in the collection.

ChildRows - returns the data rows that are currently represented by RadGridView in the order in which they appear. The collection is modified every time a data operation (grouping, sorting, filtering) occurs. 

According to the provided code snippet, it seems that the Rows collection is used which explains the observed result on your end. It is recommended to use the ChildRows collection instead which will be affected when the grid is grouped. The following help article is quite useful for understanding better the difference between the two collections: 

https://docs.telerik.com/devtools/winforms/controls/gridview/rows/rows-vs-childrows#rows-vs-childrows-example 

0
todd
Top achievements
Rank 1
answered on 01 Feb 2013, 05:25 PM
Thanks a lot, it works perfectly.
Tags
GridView
Asked by
todd
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
todd
Top achievements
Rank 1
Share this question
or