Domanda

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

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top