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

Calculate Sum function on a grouping column

4 Answers 546 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alcide Burato
Top achievements
Rank 1
Alcide Burato asked on 19 Apr 2011, 10:34 AM
Hello,
My intention is to calculate the sum of all the values of a column, where this column was selected as a grouping column.
I'd like to show the result directly in the grouping header row.

This is the code I'm using:
string ColumnName = "Price";
GroupDescriptor descriptor1 = new GroupDescriptor();
descriptor1.GroupNames.Add(ColumnName, ListSortDirection.Ascending);   
 
string formatStr  = "The total sum of items {0} with price equal to {1} is equal to {2}.";  
descriptor1.Format = formatStr;
descriptor1.Aggregates.Add("Sum('" + ColumnName + "')");
this.radGridView1.GroupDescriptors.Add(descriptor1);

In this code my intention is to calculate and show the total sum of all the items grouped by the price itself.

The problem is the result is alway zero. It seems like the GridAggregateFunction.Sum doesn't works inside the GroupDescriptor

I tryed exacly the same code using the COUNT aggregate function instead of the SUM, ad it works perfectly giving me the count of the item grouped by the price:
string ColumnName = "Price";
GroupDescriptor descriptor1 = new GroupDescriptor();
descriptor1.GroupNames.Add(ColumnName, ListSortDirection.Ascending);  
  
string formatStr  = "The total sum of items {0} with price equal to {1} is equal to {2}."
descriptor1.Format = formatStr;
descriptor1.Aggregates.Add("Count('" + ColumnName + "')");
this.radGridView1.GroupDescriptors.Add(descriptor1);

Thanks for any suggestions,
Alcide Burato

4 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 22 Apr 2011, 10:32 AM
Hello Alcide,

Your approach to accomplish these requirements is correct. There is only one little change that you should do - do not use single quotation marks for the column name in the group aggregate:
descriptor1.Aggregates.Add("Sum(" + ColumnName + ")");

You can find more examples for group aggregates in this help article. Do not hesitate to contact us again if you have additional questions.

Best regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Alcide Burato
Top achievements
Rank 1
answered on 26 Apr 2011, 08:24 AM
Hello Alexander

thanks for your answer.

I have some column names with spaces and '-' character, so without using the quotation mark an exception is raised when I group by that column.

In the examples I didn't found anything that explains how to treat column names with space (ex. "Item Price") and special characters (es. "Orders - Price Unit)".

The error is raised during the execution of the following instruction:

this.radGridView1.GroupDescriptors.Add(descriptor1);

And the error message is the following in the case of grouping and sum by the column "Orders - Price Unit":

[Telerik.Data.Expressions.ParserException] = {"Missing operator before Unit operand."}

Have you got some suggestions that could solve my problem?

Thanks,
Alcide
0
Alexander
Telerik team
answered on 28 Apr 2011, 11:22 AM
Hello Alcide,

Thank you for writing back.

The additional details which you provided helped me to understand your case. When the column name contains special characters like spaces and '-' character, you should enclose it with square brackets. The single quotes mark the expression which they enclose as a string literal. You can review this article for more information concerning the expressions syntax.

Assuming the above explanation, please try to use the following syntax in your example:
descriptor1.Aggregates.Add("Sum([" + ColumnName + "])");

I hope it helps.

Best regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Alcide Burato
Top achievements
Rank 1
answered on 28 Apr 2011, 05:14 PM
Hello Alexander,

I confirm you your suggestion about the usage of square brackets to enclose the column names has solved my problem.

Thanks for the excellent support.

Regards,
Alcide

Tags
GridView
Asked by
Alcide Burato
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Alcide Burato
Top achievements
Rank 1
Share this question
or