Change cellvalue when other cell is changed

3 Answers 1330 Views
GridView
johan
Top achievements
Rank 1
johan asked on 31 Aug 2010, 02:49 PM
Hi,

I have a grid, with 3 columns. 1 column with amount, one with nothing and one with total.
When i put and X in the column with nothing, I want the value of the total being changed into the value of the amount column. I have tried several events, but I dont quite understand how to do it. Should I use the cellformatting event? Thats the one that brings me closest to the desired solution

Thanks

3 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 03 Sep 2010, 12:11 PM
Hello johan,

I am not sure that I understand your question. However, you can choose one of the following options:

1. You can use column expressions:

this.radGridView1.Columns["Value2"].Expression = "Value + 2";

2. You can use the CellValueChanged event and change the value conditionally:

void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    if (e.Column.Name == "Value")
    {
        e.Row.Cells["Value2"].Value = (int)e.Value + 2;
    }
}

3. As you supposed, you can handle the CellFormatting event. However, in this case you will change only the visual element's text. The value in the data source will not be changed:

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.ColumnInfo.Name == "Value2")
    {
        object value = e.CellElement.RowInfo.Cells["Value"].Value;
        if (value != null && value != DBNull.Value)
        {
            e.CellElement.Text = ((int)value+2).ToString();
        }
    }
}

If this is not the case, please describe with more detail the desired behavior and send me your application. I will be glad to help further.
 
Regards, Jack
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
FAHID
Top achievements
Rank 1
commented on 18 May 2018, 04:04 PM

Here i have a gridview containg Services, Rate, ChekBox(if checkBox is Checked it means Service is used Otherwise Services Not used)  , Quantity , Amount(Rate * Quantity) 

Now I want  when i Add Quantity then Amount is Automatically Put in "Amount" Cell.

Please Help me How i do this.

I am using C#

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 22 May 2018, 08:17 AM
Hello, Fahid,    

RadGridView supports calculated columns which are suitable for your case. Additional information is available in the following help article: https://docs.telerik.com/devtools/winforms/gridview/columns/calculated-columns-(column-expressions)

Here is a sample code snippet: 

GridViewDecimalColumn rateColumn = new GridViewDecimalColumn("Rate");
radGridView1.MasterTemplate.Columns.Add(rateColumn);
 
GridViewDecimalColumn quantity = new GridViewDecimalColumn("Quantity");
radGridView1.MasterTemplate.Columns.Add(quantity);
 
GridViewDecimalColumn amount = new GridViewDecimalColumn("Total Amount");
amount.Expression = "Rate * Quantity";
radGridView1.MasterTemplate.Columns.Add(amount);
 
for (int i = 0; i < 5; i++)
{
    this.radGridView1.Rows.Add(i);
}

I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Dilshod
Top achievements
Rank 1
commented on 28 May 2018, 07:59 PM

Hey guys,

I have similar issue in master-detail Grid. In master grid level calculated field is being refreshed as expected but not in details level. Attached example:

public partial class Form1 : Form
    {
 
        public BindingList<Test> tests { get; set; }
        public Form1()
        {
            InitializeComponent();
 
            tests = new BindingList<Test>();
            tests.Add(
                new Test()
                {
                    A = 5,
                    B = 4,
                    DetailsList = new BindingList<Test>()
                    {
                        new Test
                        {
                            A = 11,
                            B = 22
                        }
                    }
                }
                );
 
            radGridView1.DataSource = tests;
            radGridView1.AutoGenerateHierarchy = true;
        }
    }
 
    public class Test : INotifyPropertyChanged
    {
        private int _a;
        private int _b;
 
        public int A
        {
            get => _a;
            set
            {
                _a = value;
                Calculate();
                OnPropertyChanged("A");
            }
        }
        public int B
        {
            get => _b;
            set
            {
                _b = value;
                Calculate();
                OnPropertyChanged("B");
            }
        }
        public int C { get; private set; }
        private void Calculate()
        {
            C = A + B;
        }
 
 
        public BindingList<Test> DetailsList { get; set; }
 
 
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 29 May 2018, 08:00 AM
Hello, Dilshod,    

In order to reflect the changes for the inner levels in RadGridView, you can handle the CellValueChanged event and refresh the relevant template:
private void RadGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
{
    if (e.Row.HierarchyLevel == 1)
    {
        this.radGridView1.MasterTemplate.Templates[0].Refresh();
    }
 
}

I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
johan
Top achievements
Rank 1
Answers by
Jack
Telerik team
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or