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

Get summary of columns

12 Answers 466 Views
GridView
This is a migrated thread and some comments may be shown as answers.
HamiD Mayeli
Top achievements
Rank 1
HamiD Mayeli asked on 28 Jan 2010, 11:12 AM
Hi.

I wanna get summary of columns, but i cant take it with foreach loop cuz when end user change cells value i have to execute loop repeatedly and i will lost performances.

in ValueChanging event i have Old And New Value and can calculate my summary but i cant distinguish cells to select correct cell for change my sum.

best regards.

12 Answers, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 02 Feb 2010, 09:53 AM
Hi HamiD Mayeli,

There are two summaries. The first ones are similar to those of SQL aggregates fucntions -- calculated column. The second ones are group expressions i.e. evaluated for each group, but as far as I understand your question, you need calculated columns as in the online documentation article.

Regards,
Nick
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Julian Benkov
Telerik team
answered on 02 Feb 2010, 10:04 AM
Hello HamiD Mayeli,

You can use SummaryRows for calculation:

private void LoadFreightSummary()
{
    GridViewSummaryRowItem item1 = new GridViewSummaryRowItem();           
    item1.Add(new GridViewSummaryItem("Freight", "Sum: {0:F2}; ", GridAggregateFunction.Sum));           
    this.radGridViewDemo.MasterGridViewTemplate.SummaryRowsBottom.Add(item1);           
    GridViewSummaryRowItem item2 = new GridViewSummaryRowItem();           
    item2.Add(new GridViewSummaryItem("Freight", "Min: {0:F2}", GridAggregateFunction.Min));           
    this.radGridViewDemo.MasterGridViewTemplate.SummaryRowsTop.Add(item2);       
}

In order to enable group summaries, add GridViewSummaryRowItems to one of the following collections: GridViewTemplate.SummaryRowGroupHeaders, GridViewTemplate.SummaryRowsTop or GridViewTemplate.SummaryRowsBottom. In each GridViewSummaryRowItem, add a GridViewSummaryRowItem object for each summary you need. Initialize them by setting their FieldName property to the corresponding column and Function to the desired function. You can also control the text formatting by changing the FormatString.

Greetings,
Julian Benkov
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
HamiD Mayeli
Top achievements
Rank 1
answered on 02 Feb 2010, 07:05 PM
Hi,

tanx alot Julian Benkov and Nick, but i have 2 Grid on my form and wanna to take sum of one or two column(s) of each of them and show sum of this two number in a label, and change label.text when value of a cell was change.

I`m try GridViewSummaryRowItem but in this case i have a GridViewSummaryRowItem and need a single value like an int or a long variant.

Regards.
0
Julian Benkov
Telerik team
answered on 04 Feb 2010, 09:16 AM
Hello HamiD Mayeli,

Currently, RadGridView control does not support this functionality natively. You must make your calculation manually with cycling rows. In the next major edition of RadGridView we extend the API for aggregates and add Evaluate method, which will be used to calculate values based on expressions:

 

this.radGridView1.Evaluate("Sum(price) * tax", this.radGridView1.Rows);

All the best,

Julian Benkov
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
HamiD Mayeli
Top achievements
Rank 1
answered on 04 Feb 2010, 03:47 PM
Hello Julian Benkov

How i can know value of  a cell was changed? in ValueChanging Event i cant distinguish Cells.

Best Wishes.
0
Nick
Telerik team
answered on 05 Feb 2010, 08:46 AM
Hi HamiD Mayeli,

Thank you for contacting us back. You need CellValueChanged as described in our documentation.

Do not hesitate to write me back if you have further questions.

Best wishes,
Nick
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
HamiD Mayeli
Top achievements
Rank 1
answered on 06 Feb 2010, 03:37 PM
Hi,

In CellValueChanged I have new value only but i need both of new and old value to calculate sum of column in evrey value changing.


Tanx alot,
Best Regards.
0
Nick
Telerik team
answered on 10 Feb 2010, 08:51 AM
Hello HamiD Mayeli,

Thank you for contacting us back. Please send us a code snippet of what you have currently written and a description as I am not sure how I can help you in the best possible way. If you need both the old and the new value perhaps you need CellValidating. I am looking forward to your reply.

Kind regards,
Nick
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
HamiD Mayeli
Top achievements
Rank 1
answered on 10 Feb 2010, 07:20 PM
Hello Nick,
Thancks alot for your and your`s team anwsers

I have a Grid with this columns (Qty, Rate, Amount, ...). I wanna calc Amount and Qty columns`s Sum.
if (sender is Telerik.WinControls.UI.GridViewCellInfo)  
            {  
// this section execute when cell value change with code  
// in this part i have not Old Value, And cell.Value is equal new value  
                Telerik.WinControls.UI.GridViewCellInfo cell = (Telerik.WinControls.UI.GridViewCellInfo)sender;  
                MessageBox.Show("1." + cell.ColumnInfo.FieldName + Environment.NewLine +  
                    "2." + e.OldValue + Environment.NewLine + "3." + e.NewValue + Environment.NewLine +  
                    "4." + cell.Value.ToString());  
            }  
            else 
            {  
// this section execute when cell value change with users  
// in this part i have both of Old Value And new value  
                Telerik.WinControls.UI.GridDataCellElement cell = (Telerik.WinControls.UI.GridDataCellElement)sender;  
                MessageBox.Show("1-" + cell.ColumnInfo.HeaderText + Environment.NewLine +  
                    "2-" + e.OldValue + Environment.NewLine + "3-" + e.NewValue + Environment.NewLine +   
                    "4-" + cell.Value.ToString());  
            } 

In this event sender parameter has 2 type
1- Telerik.WinControls.UI.GridViewCellInfo: when Value Change with code like "Grid.CurrentRow.Cells[0].Value = 1"
2- Telerik.WinControls.UI.GridDataCellElement: when Value Change with users
in my Grid Amount is equal Qty * Rate so when Qty or Rate was change Amount will change and i will lost true sum.

Regard,
0
Nick
Telerik team
answered on 11 Feb 2010, 10:33 AM
Hello HamiD Mayeli,

Thank you for providing these details. I think that there is a simpler way of implementing that. Please correct me if I am wrong.

The code snippet below demonstrates how you can evaluate the value of Amount automatically based on the value of the other two columns:

01.public partial class Form1 : Form
02.    {
03.        public Form1()
04.        {
05.            InitializeComponent();
06.        }
07.        DataTable t = new DataTable();
08.  
09.        private void Form1_Load(object sender, EventArgs e)
10.        {
11.            t.Columns.Add("Rate", typeof(decimal));
12.            t.Columns.Add("Quantity", typeof(decimal));
13.            t.Columns.Add("Amount", typeof(decimal));
14.  
15.            this.radGridView1.DataSource = t;
16.            this.radGridView1.Columns["Amount"].Expression = "Rate * Quantity";
17.            this.radGridView1.Columns["Amount"].ReadOnly = true;
18.        }
19.    }

You need to add an expression that multiplies the two values. I have made the amount column read-only so that it cannot be changed by the user e.g. keyboard, but only by values in the other two columns.

All the best,
Nick
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
HamiD Mayeli
Top achievements
Rank 1
answered on 14 Feb 2010, 05:10 PM
Hi Nick,

I can`t use Expression Cuz i wanna user can insert qty and Amount And I Calculate Rate.
Finally I Can do what i want, in CellValueChanging if Fire MessageBox.Show change will cancel automatically
i used this Code

private decimal sumOfAmountColumn = 0;  
        private decimal sumOfQtyColumn = 0;  
        private decimal sumOfCashColumn = 0;  
        private decimal SumOfAmountColumn  
        {  
            get 
            {  
                return sumOfAmountColumn;  
            }  
            set 
            {  
                sumOfAmountColumn = value;  
                lblSumOfAmount.Text = Convert.ToInt64(value).ToString();  
            }  
        }  
 
        private decimal SumOfQtyColumn  
        {  
            get 
            {  
                return sumOfQtyColumn;  
            }  
            set 
            {  
                sumOfQtyColumn = value;  
                lblSumOfQty.Text = Convert.ToInt64(value).ToString();  
                SetRamainVoucher();  
            }  
        }  
 
        private decimal SumOfCashColumn  
        {  
            get 
            {  
                return sumOfCashColumn;  
            }  
            set 
            {  
                sumOfCashColumn = value;  
                lblSumOfCash.Text = Convert.ToInt64(value).ToString();  
                SetRamainVoucher();  
            }  
        }  
 
private void GridItm_ValueChanging(object sender, Telerik.WinControls.UI.ValueChangingEventArgs e)  
        {  
            if (sender is Telerik.WinControls.UI.GridViewCellInfo)  
            {  
                Telerik.WinControls.UI.GridViewCellInfo cell = (Telerik.WinControls.UI.GridViewCellInfo)sender;  
                switch (cell.ColumnInfo.UniqueName)  
                {  
                    case "Amount":  
                        SumOfAmountColumn += Convert.ToDecimal(e.NewValue) - Convert.ToDecimal(IfNull(cell.Value, 0));  
                        break;  
 
                    case "Qty":  
                        SumOfQtyColumn += Convert.ToDecimal(e.NewValue) - Convert.ToDecimal(IfNull(cell.Value, 0));  
                        break;  
 
                    default:  
                        return;  
                }  
            }  
            else 
            {  
                Telerik.WinControls.UI.GridDataCellElement cell = (Telerik.WinControls.UI.GridDataCellElement)sender;  
                switch (cell.ColumnInfo.HeaderText)  
                {  
                    case "Amount":  
                        SumOfAmountColumn += Convert.ToDecimal(e.NewValue) - Convert.ToDecimal(IfNull(cell.Value, 0));  
                        break;  
 
                    case "Qty":  
                        SumOfQtyColumn += Convert.ToDecimal(e.NewValue) - Convert.ToDecimal(IfNull(cell.Value, 0));  
                        break;  
 
                    default:  
                        return;  
                }  
            }  
        } 
0
Nick
Telerik team
answered on 16 Feb 2010, 09:23 AM
Hello HamiD Mayeli,

Thank you for contacting us back. I am glad that you implemented your scenario. If you find something else that you want to ask about, do not hesitate to write me back.

Regards,
Nick
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.
Tags
GridView
Asked by
HamiD Mayeli
Top achievements
Rank 1
Answers by
Nick
Telerik team
Julian Benkov
Telerik team
HamiD Mayeli
Top achievements
Rank 1
Share this question
or