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

Gridview summary row conditional sum

7 Answers 409 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mitul
Top achievements
Rank 1
Mitul asked on 21 Feb 2012, 05:46 PM
I am still using a trial version of GridView to get an idea how powerful the control is. So, far everything is going good except I have no idea how to handle following situation.
The data is as follow

stock1 B 15
stock1 S 45
stock1 B 67
stock1 S  2

Where S-Sell and B-Buy. While grouping on column 1 (stock1) I need to display the position in stock1. But to do that while summing up the result the sell qty should be considered negative. How can I customize this sum operation?

Mitul

7 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 21 Feb 2012, 06:25 PM
Hello,

If you have a look at this help topic on Summary Rows and see the last example, this shows how to customise the summary row.
Hope that helps
Richard
0
Mitul
Top achievements
Rank 1
answered on 21 Feb 2012, 06:40 PM
Thanks for the prompt response.

I already tried that before posting the question. Here is the code that I am using. I have made one modification though where it checks whether the side has valid value or not. Although all of my rows in the gridview has side and filledqty populated after groping it throws exception if I don't put the null checking condition.


By the way the null condition occurs when I group the data by dragging exchange and product.

 public class CustomSummaryItem : GridViewSummaryItem
    {
        public CustomSummaryItem()
            : base()
        { }

        public override object Evaluate(IHierarchicalRow row)
        {
            int totalPos = 0;
            foreach (GridViewRowInfo childRow in row.ChildRows)
            {
               object objSide= childRow.Cells["Side"].Value;
               if (objSide != null) /// if I don' know why but without this condition it throws null exception while retrieving FIlledQty.
               {
                   if ((string)objSide == "B")
                   {
                       totalPos += (int)childRow.Cells["FilledQty"].Value;
                   }
                   else
                   {
                       totalPos -= (int)childRow.Cells["FilledQty"].Value;
                   }
               }
            }
            return totalPos;
        }
    }
0
Mitul
Top achievements
Rank 1
answered on 23 Feb 2012, 06:51 PM
The solution which worked for me is the following one. Basically what happens is when you remove/add a group childrows might have two types of rows one with data and one with of type GridViewGroupRowInfo type which  does not have data but only the group total.


The only problem now is I have a summary row on top which shows the total correctly but as soon as the view is grouped and any of the items gets updated the main summary row total goes to 0.


  public class CustomSummaryItem : GridViewSummaryItem
    {
        public CustomSummaryItem()
            : base()
        { }

        public override object Evaluate(IHierarchicalRow row)
        {
            int totalPos = 0;
            foreach (GridViewRowInfo childRow in row.ChildRows)
            {
                if ((childRow is GridViewGroupRowInfo) == false)
                {

                    object objSide = childRow.Cells["Side"].Value;
                    string type = Convert.ToString(childRow.Cells["ProdType"].Value);
    
                        if (type == "FUTURE")
                        {
                            if ((string)objSide == "B")
                            {
                                totalPos += (int)childRow.Cells["FilledQty"].Value;
                            }
                            else
                            {
                                totalPos -= (int)childRow.Cells["FilledQty"].Value;
                            }
                        }
                    }
    
            }
            return totalPos;
        }
    }

Thanks,

Mitul Patel
özer
Top achievements
Rank 2
Veteran
Iron
commented on 11 May 2023, 05:52 PM

Hi.

Except for one minor bug this solution worked for me too.

I do not want the selected row in the image below to be included in the total.

For this, I adapted the solution you sent to my own project as follows.


public class CustomSummaryItem : GridViewSummaryItem
    {
        public CustomSummaryItem()
            : base()
        { }

        public override object Evaluate(IHierarchicalRow row)
        {
            decimal totalPos = 0;
            foreach (GridViewRowInfo childRow in row.ChildRows)
            {
                if ((childRow is GridViewGroupRowInfo) == false)
                {

                    string bm = childRow.Cells["bm"].Value.ToString();

                    if (bm != "Ä°PTAL")
                    {
                        totalPos += Convert.ToDecimal(childRow.Cells[this.Name].Value);
                    }
                }

            }
            return totalPos;
        }
    }

As seen in the image below, the grand total row fixed to the bottom of the grid does not work as expected.

Why does this happen and how can it be fixed?

Thanks in advance for the responses.

Dinko | Tech Support Engineer
Telerik team
commented on 16 May 2023, 08:14 AM

This question seems to be a duplicate of another forum thread created from your account. Let's continue there: Gridview summary row use sumif like excel formula. I have already posted my reply with a possible solution.
0
Julian Benkov
Telerik team
answered on 24 Feb 2012, 02:46 PM
Hi Mitul,

Please open a new support ticket and send us a sample project that demonstrates the described behavior with some part of your data and grouping settings applied to RadGridView. This will allow us to investigate the issue locally. 

Thank you for yout time and cooperation.

Kind regards,
Julian Benkov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Nikolay
Telerik team
answered on 24 Feb 2012, 03:09 PM
Hi Mitul,

Please open a new support ticket and send us a sample project that demonstrates the described behavior with some part of your data and grouping settings applied to RadGridView. This will allow us to investigate the issue locally. 

Thank you for yout time and cooperation.

Kind regards,
Nikolay
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Mitul
Top achievements
Rank 1
answered on 24 Feb 2012, 06:21 PM
Thanks for the response admins. I am sorry but I won't be able to share the code of the proprietary trading application. I have explained the grouping code that I am using over here. I have also posted few other issues in that ticket.

Thanks,

Mitul Patel
0
Nikolay
Telerik team
answered on 29 Feb 2012, 09:58 AM
Hi Mitul,

You can find our answer in the other thread that you have opened. Please note that it has been converted to a support thread and you will not be able to find it as a forum thread. You can find it in your account after you log in.

Kind regards,
Nikolay
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Mitul
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Mitul
Top achievements
Rank 1
Julian Benkov
Telerik team
Nikolay
Telerik team
Share this question
or