calculate totalprice base on checkbox
-
Hi, i got this code to calculate the price when i tick the checkbox in my datagridview
double price = 0.0; foreach (DataGridViewRow dgw in dataGridProduct.Rows) { DataGridViewCheckBoxCell datacell = dgw.Cells\[0\] as DataGridViewCheckBoxCell; try { if ((bool)datacell.Value == true) { //MessageBox.Show("" + dgw.ToString()); price += Convert.ToDouble(dgw.Cells\[6\].Value.ToString()); lblTotalPrice.Text = "Total Price : $ " + price; } } catch (Exception exc) { } }
When i tick the box, the price should add up, and when i untick the price will be minus. I believe abit of my logic is wrong, can you help me with it?
-
Hi, i got this code to calculate the price when i tick the checkbox in my datagridview
double price = 0.0; foreach (DataGridViewRow dgw in dataGridProduct.Rows) { DataGridViewCheckBoxCell datacell = dgw.Cells\[0\] as DataGridViewCheckBoxCell; try { if ((bool)datacell.Value == true) { //MessageBox.Show("" + dgw.ToString()); price += Convert.ToDouble(dgw.Cells\[6\].Value.ToString()); lblTotalPrice.Text = "Total Price : $ " + price; } } catch (Exception exc) { } }
When i tick the box, the price should add up, and when i untick the price will be minus. I believe abit of my logic is wrong, can you help me with it?
Check the state of the checkbox and use the appropriate operator.
if(checkbox1.checked)
price += Convert.ToDouble(dgw.Cells[6].Value.ToString());
else
price -= Convert.ToDouble(dgw.Cells[6].Value.ToString());Just because we can; does not mean we should.
-
Hi, i got this code to calculate the price when i tick the checkbox in my datagridview
double price = 0.0; foreach (DataGridViewRow dgw in dataGridProduct.Rows) { DataGridViewCheckBoxCell datacell = dgw.Cells\[0\] as DataGridViewCheckBoxCell; try { if ((bool)datacell.Value == true) { //MessageBox.Show("" + dgw.ToString()); price += Convert.ToDouble(dgw.Cells\[6\].Value.ToString()); lblTotalPrice.Text = "Total Price : $ " + price; } } catch (Exception exc) { } }
When i tick the box, the price should add up, and when i untick the price will be minus. I believe abit of my logic is wrong, can you help me with it?
What problem are you having? One problem I can see is that you don't have any logic listed to do the price subtraction.
Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns
-
Hi, i got this code to calculate the price when i tick the checkbox in my datagridview
double price = 0.0; foreach (DataGridViewRow dgw in dataGridProduct.Rows) { DataGridViewCheckBoxCell datacell = dgw.Cells\[0\] as DataGridViewCheckBoxCell; try { if ((bool)datacell.Value == true) { //MessageBox.Show("" + dgw.ToString()); price += Convert.ToDouble(dgw.Cells\[6\].Value.ToString()); lblTotalPrice.Text = "Total Price : $ " + price; } } catch (Exception exc) { } }
When i tick the box, the price should add up, and when i untick the price will be minus. I believe abit of my logic is wrong, can you help me with it?
You are missing code for when the checkbox is false. Look at the else clause that you have been shown...
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon