如何在datagridview中将第二列的两列数据相乘

我想将两列的数据相乘并在第三列中显示它。

例如:

1st Column: Quantity 2nd Column: Rate 3rd Column: Price 

我希望乘以用户输入数量和Quantity=2的数据,如Quantity=2rate=50自动在价格列我希望100出现。

同时我想分割为用户输入数量和Price=100的数据,如Price=100rate=50自动在Quantity栏中我要2出现。

当用户输入数量和Price=100的数据,如Price=100Quantity=2自动在费率列我想要50出现。

这三个将在同一个datagridview中发生。 用户只能输入这三个中的任意两个字段,第三个字段将自动进入。

使用C#,VS2008,SQL 2008

 private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e) { int quantity,rate; for (int i = 0; i < dataGridView2.Rows.Count; i++) { if(int.TryParse(dataGridView2.Rows[i].Cells[1].Value.ToString(), out quantity) && int.TryParse(dataGridView2.Rows[i].Cells[2].Value.ToString(), out rate)) { int price = quantity * rate; dataGridView2.Rows[i].Cells[3].Value = price.ToString(); } } } 

我写这段代码。 显示错误。 对象引用未设置为对象的实例

 if(int.TryParse(dataGridView2.Rows[i].Cells[1].Value.ToString(), out quantity) && int.TryParse(dataGridView2.Rows[i].Cells[2].Value.ToString(), out rate)) 

这条线。


在此处输入图像描述

我想这样做。 用户可以填写其中的任何2个字段。第三个字段将自动填充。

处理gridview的CellEndEdit事件。 在那种情况下,计算并将值分配给第三列

就像是

 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { int quantity,rate; if (int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value.ToString(), out quantity) && int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["rate"].Value.ToString(), out rate)) { int price = quantity * rate; dataGridView1.Rows[e.RowIndex].Cells["price"].Value = price.ToString(); } }