Question

manipulating data without sql or another table is hard..

this.dataGridView1.Rows.Add("Room: " + label40.Text, type, textBox1.Text + "m", textBox2.Text + "m", label10.Text, label9.Text, "$" + label38.Text);

...

lblTotalPrice.Text = Convert.ToString(
dataGridView1.Rows.Cast<DataGridViewRow>()
             .Select(x => Int32.Parse(x.Cells[6].ToString(),
                                      ???))
             .Sum());

errors

Était-ce utile?

La solution

This is untested, but should work:

lblTotalPrice.Text = Convert.ToString(
    dataGridView1.Rows.Cast<DataGridViewRow>()
                 .Select(x => Int32.Parse(x.Cells[6].Value.ToString(),
                                          NumberStyles.AllowCurrencySymbol))
                 .Sum());

It takes the 6th column of your DataGrid, which should be Cost, then converts the string value back to a number and adds them up.

Execute this code as needed to update the total, such as when you click the "Add New Room" button.

Autres conseils

handle the event RowsAdded of the DataGridView.

as stated on msdn the event is called when a new row is added (no reference to difference between rows added programmatically and by databinding so it should work in your case); for each new row you sum all the values and then fill the box vith the total.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top