3

I am using a RadGridView to display items that are being sold. In the same row I also have a "Quantity", "Unit Price", "Total Price" column.

When a user change the value of the "Quantity" Column I want to fire an event that will calculate the value of the "Total Price" column by multiplying the "Quantity" by the "Item Price"

How can add such an event that will only be triggered if the value of the column "Quantity" is changed?

I have tried this which had no affect

private void radGridView1_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e) {

    double itemPrice = Convert.ToDouble(e.CurrentRow.Cells["Unit Price"].Value);
    int itemQty = Convert.ToInt32(e.CurrentRow.Cells["Qty"].Value);
    double totalPrice = itemPrice * itemQty;

    e.CurrentRow.Cells["Total Price"].Value = totalPrice.ToString();
}

enter image description here

Jaylen
  • 39,043
  • 40
  • 128
  • 221

1 Answers1

2

Subscribe to the CellEndEdit event (in your constructor if you want):

radGridView1.CellEndEdit += (s, e) =>
{
    if (e.Column == radGridView1.Columns["Qty"])
    {
        var row = radGridView1.CurrentRow.Cells;

        row["Total Price"].Value =
            (int)row["Qty"].Value * (decimal)row["Item Price"].Value;
    }
};

You may want to add some error handling, and cast to different types if price isn't a decimal, etc.

You could split it out into a separate method too; with a short method, I sometimes find this "inline" way easier to read and maintain. YMMV.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • I have added this code into the Form_Load but it is not firing when the quantity column change. – Jaylen Jan 25 '15 at 03:35
  • I just added a screen shot in my question to show you visually how I am making the edit. after I finish editing I hit enter or change the focus to a different cell/control – Jaylen Jan 25 '15 at 03:41
  • Thank you so much. it was indeed a typo – Jaylen Jan 25 '15 at 05:12