문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top