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

How to dynamcially calculate values in cells

5 Answers 259 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Afraz Ali
Top achievements
Rank 1
Afraz Ali asked on 12 Mar 2011, 11:53 AM

Hi All,

I have a grid in which I add products (Screen shot attached). I want to calculate some values based on values in some cells. Here is my scenerio

When the value in Qty cell is changed I want to calculate the value of Amount column (Amount = Qty * Unit price)

The first element in my Unit combobox column should be selected by default when a new row is added but that is not the case, I have to manually select the item as shown in row2 of screen shot.

I can't seem to find the RowAdded event or something similar which I am so used to of using in ASP.Net controls.

 

I know that if I add the textboxcolumn from code behind then I can handle its textchange event, but I have added column through “Property Builder” and can’t seem to find the controls from the row.

Note: I am using Q2 2010 SP2

Can anyone please help me.

Best Regards,

Afraz

5 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 12 Mar 2011, 02:56 PM
Hello,

there are several ways thart you could make your calculations depending on what it is you wish to do. (sorry, the screenshot sisn't seem to be there on your post).

Please could you let me know a bit more about what you want to do. My first guess is that you wish to have a calculated column to calculate some other columns. If that's the case, then you might wish to have a look at this help topic on calculated columns.

You can also calculate at the start of end of a row using a summary row

If neither of these is what you're looking for, or if you need more information, then please let me know
Thanks
Richard
0
Afraz Ali
Top achievements
Rank 1
answered on 14 Mar 2011, 05:31 AM
Hello Richard,

Thanks for your reply. Sorry due to some reason the Screen Shot didn't get attached with the last post. I have attached it now. Actually what i would ideally want is to get reference of the text box control in the TextBoxColumn cells and the Drop down control in the DropDownListCell. Then I can write code agains there Text changed event and Selected Index changed event.
Here is my scenerio in detail.

My grid is not bound to a datasource instead I am manually adding products and there prices in it based on the user selection.

I would like the user to only enter numeric values in Qty column and Decimal values in Disp Price & Disc % columns.

Secondly whenever user changes value in Qty column the amount column should be updated accordingly.

Finally the first unit in Units column (i.e Kg(s)) should be selected by default, which is not the case.

I am really struck here at these points and would really appretiate your help.

Best Reagards,

Syed Afraz Ali

0
Afraz Ali
Top achievements
Rank 1
answered on 14 Mar 2011, 07:03 AM
" I would like the user to only enter numeric values in Qty column and Decimal values in Disp Price & Disc % columns."

I figured out how to allow only numeric values. I added MaskedTextBoxColumn and set its Masked property to "f". This seems to work perfectly. The other questions are still open.

Afraz
0
Emanuel Varga
Top achievements
Rank 1
answered on 14 Mar 2011, 07:50 AM
Hello guys,

Please take a look at the following example:

using System.Collections.Generic;
 using System.Windows.Forms;
 using Telerik.WinControls.UI;
 
 public partial class Form1 : Form
 {
     private RadGridView radGridView1;
     private List<DataType> dataTypes;
     private List<Product> products;
 
     public Form1()
     {
         InitializeComponent();
         this.Controls.Add(radGridView1 = new RadGridView());
         radGridView1.Dock = DockStyle.Fill;
         radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
         dataTypes = new List<DataType>();
         dataTypes.Add(new DataType
         {
             Id = 1,
             Name = "g",
         });
 
         dataTypes.Add(new DataType
         {
             Id = 2,
             Name = "Kg",
         });
 
         products = new List<Product>();
         products.Add(new Product{Name = "SomeName", Qty = 10, Price = 100, TypeId =  1});
         products.Add(new Product{Name = "OTherName", Qty = 20, Price = 9.99M, TypeId =  2});
 
         radGridView1.AutoGenerateColumns = false;
         radGridView1.Columns.Add(new GridViewTextBoxColumn("Name"));
         var qtyColumn = new GridViewDecimalColumn("Qty");
         qtyColumn.DataType = typeof(int);
         radGridView1.Columns.Add(qtyColumn);
 
         var typeColumn = new GridViewComboBoxColumn("TypeId");
         typeColumn.FieldName = "TypeId";
         typeColumn.ValueMember = "Id";
         typeColumn.DisplayMember = "Name";
         typeColumn.DataSource = dataTypes;
         radGridView1.Columns.Add(typeColumn);
 
         var decimalColumn = new GridViewDecimalColumn("Price");
         radGridView1.Columns.Add(decimalColumn);
 
         var calculatedColumn = new GridViewDecimalColumn("TotalPrice");
         calculatedColumn.Expression = "Qty * Price";
         radGridView1.Columns.Add(calculatedColumn);
 
         radGridView1.DataSource = products;
     }
 }
 
 public class DataType
 {
     public int Id
     {
         get;
         set;
     }
 
     public string Name
     {
         get;
         set;
     }
 }
 
 public class Product
 {
     public string Name
     {
         get;
         set;
     }
 
     public int Qty
     {
         get;
         set;
     }
 
     public decimal Price
     {
         get;
         set;
     }
 
     public int TypeId
     {
         get;
         set;
     }
 }

I believe it incorporates everything you need

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Telerik WinForms MVP
0
Afraz Ali
Top achievements
Rank 1
answered on 14 Mar 2011, 10:25 AM
Hi Emanuel,

Thanks for your reply. Your post and this post solved my problem perfectly. It turned out, my logic for calculating amount is a bit complex and a simple expression would not have worked for me. So I used the approach mentioned in this post.

http://www.telerik.com/community/forums/winforms/gridview/selectedindexchanged-in-gridviewcomboboxcolumn.aspx

Best Regards,
Afraz
Tags
GridView
Asked by
Afraz Ali
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Afraz Ali
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Share this question
or