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

Calculated Column Problem

10 Answers 235 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Emma
Top achievements
Rank 1
Emma asked on 28 Sep 2010, 11:22 PM
Hi
I have two columns on my gridview that need to be calculated. Column One is (A - B) Column B is (Result of Column One * Qty Ordered).
No matter how I write this I am getting an error saying "cannot remove this column because it is part of an expression "A - B""
How do I get around this issue.
Thanks
Em

10 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Sep 2010, 07:24 AM
Hello Emma,

I've prepared a test form that creates two columns, the first just adds the two bounded columns, and the second one multiplies the first calculated column by two, please take a look at this and tell me if there is something else i can help you with:
namespace GridCalculatedColumn
{
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
        }
 
        void Form1_Load(object sender, EventArgs e)
        {
            this.radGridView1.DataBindingComplete += new Telerik.WinControls.UI.GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
            this.radGridView1.DataSource = new TestsCollection(10);
        }
 
        void radGridView1_DataBindingComplete(object sender, Telerik.WinControls.UI.GridViewBindingCompleteEventArgs e)
        {
            var col1 = new GridViewDecimalColumn("Total1");
            col1.Expression = "Number1 + Number2";
            radGridView1.Columns.Add(col1);
 
            var col2 = new GridViewDecimalColumn("Total2");
            col2.Expression = "Total1 * 2";
            radGridView1.Columns.Add(col2);
        }
    }
 
    #region Helpers
 
    public class Test
    {
        public Test(int num1, int num2)
        {
            this.Number1 = num1;
            this.Number2 = num2;
        }
 
        public int Number1
        {
            get;
            set;
        }
 
        public int Number2
        {
            get;
            set;
        }
    }
 
    public class TestsCollection : BindingList<Test>
    {
        public TestsCollection(int noItems)
        {
            for (int i = 0; i < noItems; i++)
            {
                this.Add(new Test(i, i + 3));
            }
        }
    }
 
    #endregion
}

I have to specify that i'm using the latest version of telerik controls, Q2 2010 Sp2, if you are getting the same error with this example code, please update to the latest version.

If you need any more help, please do not hesitate.

Best Regards,
Emanuel Varga
0
Emma
Top achievements
Rank 1
answered on 06 Oct 2010, 03:20 AM
Thanks for that I have been trying  afew different ways with your code but not having any luck.

My code snippet looks like this

InitializeComponent();
                this.grdEntityList.Columns["UnitPurchasePriceVariance"].Expression = "UnitCost07 - UnitCost04";
                this.grdEntityList.Columns["TotalPurchasePriceVariance"].Expression = "UnitPurchasePriceVariance * QtyOrdered";

It is the last line that seems to be causing the problems

Thanks
Em
0
Emanuel Varga
Top achievements
Rank 1
answered on 06 Oct 2010, 05:33 AM
Hello again Emma,

What i don't understand is this, are you setting those expressions on data binding complete or not? From the snippet you've sent me i have to assume that you are doing it on the forms constructor? Please try moving that code to the DataBindingComplete event, because you may not have any data bounded to that grid at that time.

Second, Why and where are you trying to remove the UnitPurchasePriceVariance column?

Best Regards,
Emanuel Varga
0
Emma
Top achievements
Rank 1
answered on 06 Oct 2010, 05:47 AM
Hi Emmanuel
I have it at the moment in the form load event on the grid. I will move it and have a look.
I am not tryiing to remove the column that is just the error message I am getting which is why I am so confused, because the expression for the UnitPurchasePriceVariance column works fine and gives the correct result it is when I try to use the result in this column that I fall over.
Sorry if I am not explaining this correctly I am new to forums but do appreciate your help
Cheers
Em
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 06 Oct 2010, 06:05 AM
Hello Em,

I have prepared a new example, that i think would more closely resemble the data you are working with, if / where you are making things differently please make the changes on this example, so that we can solve your problems faster and better:

     using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
 
    public partial class Form1 : Form
    {
        private RadGridView radGridView1 = new RadGridView();
 
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
        }
 
        void Form1_Load(object sender, EventArgs e)
        {
            radGridView1.Dock = DockStyle.Fill;
            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.Controls.Add(radGridView1);
 
            this.radGridView1.DataBindingComplete += new Telerik.WinControls.UI.GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
            this.radGridView1.DataSource = new TestsCollection(10);
 
            var button = new RadButton();
            button.Text = "Refresh data from db...";
            button.Dock = DockStyle.Bottom;
            button.Click += new EventHandler(button_Click);
            this.Controls.Add(button);
        }
 
        void button_Click(object sender, EventArgs e)
        {
            this.radGridView1.DataSource = new TestsCollection(10);
        }
 
        void radGridView1_DataBindingComplete(object sender, Telerik.WinControls.UI.GridViewBindingCompleteEventArgs e)
        {
            radGridView1.Columns["NumberVariance"].Expression = "Price2 - Price1";
 
            radGridView1.Columns["TotalVariance"].Expression = "NumberVariance * Quantity";
        }
    }
 
    #region Helpers
 
    public class Test
    {
        public Test(int qty, int price1, int price2)
        {
            this.Quantity = qty;
            this.Price1 = price1;
            this.Price2 = price2;
        }
 
        public int Quantity { get; set; }
 
        public int Price1
        {
            get;
            set;
        }
 
        public int Price2
        {
            get;
            set;
        }
 
        public int NumberVariance { get; set; }
 
        public int TotalVariance { get; set; }
    }
 
    public class TestsCollection : BindingList<Test>
    {
        public TestsCollection(int noItems)
        {
            for (int i = 1; i <= noItems; i++)
            {
                this.Add(new Test(i * 3, i, i + 3));
            }
        }
    }
 
    #endregion Helpers


P.S. What version of telerik are you using? If you are not using the latest one, Q2 2010 SP2, please try updating to that version as soon as possible, because there were a lot of changes regarding the grid lately.

Best Regards,
Emanuel Varga
0
Alexander
Telerik team
answered on 11 Oct 2010, 03:51 PM
Hello Emma,

Thank you for your question.

I am not able to reproduce the issue you report. If Emanuel's examples do not help you to demonstrate it, please open a new support ticket and attach your project to it. It will help me to investigate this issue further.

I am looking forward to your reply.

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
Emma
Top achievements
Rank 1
answered on 11 Oct 2010, 09:54 PM
Hi
Thanks for getting back to me, Emmanuels thread doesn't work unfortunately and as the form is a grid I cannot add buttons to it.
The code I have added is this

 

public frmPurchaseOrderList()

 

{

 

Display.ResetExceptionMessage();

 

 

try

 

{

InitializeComponent();

this.grdEntityList.Columns["UnitPurchasePriceVariance"].Expression = "UnitCost07 - UnitCost04";

 

this.grdEntityList.Columns["TotalPurchasePriceVariance"].Expression = "UnitPurchasePriceVariance * QtyOrdered";

 

 

 

}

 

catch (Exception ex)

 

{

 

 

Standard exception handling

 

 

}

 

}

This line of code works great no worries

this.grdEntityList.Columns["UnitPurchasePriceVariance"].Expression = "UnitCost07 - UnitCost04";

 


When I add the line of code for the second calculated column
i.e
this.grdEntityList.Columns["TotalPurchasePriceVariance"].Expression = "UnitPurchasePriceVariance * QtyOrdered";

When I click the button to load the form I get the message "cannot remove this column because it is part of an expression UnitPurchasePriceVariance"
Nowhere in the code do I ask it to remove this cvolumn I just want to use it to calculate the second column???

Hope this makes sense and you can help
Cheers
Emma

 

 


0
Emanuel Varga
Top achievements
Rank 1
answered on 11 Oct 2010, 10:43 PM
Hello again Emma,

Can you please move that code from the Constructor to the DataBindingComplete event? I'm thinking you are rebinding the grid somewhere and it's removing it's columns, that's why you have that exception, but without more code it's very hard to tell where is the problem originating.

You could try also registering for radGridView1.Columns.CollectionChanging or CollectionChanged event in order to see what's happening in the underlying data structure.

Best Regards,
Emanuel Varga
0
Emma
Top achievements
Rank 1
answered on 11 Oct 2010, 11:03 PM
Brilliant Emmanuel
Thank you so much for your help, works well now I have it in the right place :-)
Thanks again
Emma
0
Emanuel Varga
Top achievements
Rank 1
answered on 11 Oct 2010, 11:06 PM
Hello again Emma,

Glad to be of assistance,

If you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Tags
GridView
Asked by
Emma
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Emma
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or