【发布时间】:2013-09-10 03:29:33
【问题描述】:
我想将两列的数据相乘,显示在第三列。
例如:
1st Column: Quantity
2nd Column: Rate
3rd Column: Price
当用户在价格列中自动输入Quantity=2、rate=50等数量和费率数据时,我想乘以我希望出现100。
同时,当用户在数量列中自动输入Price=100、rate=50等数量和费率数据时,我想进行除法,我希望出现2。
并且
当用户在速率列中自动输入Price=100、Quantity=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))
这一行。
我想这样做。用户可以填写这 3 个字段中的任意 2 个字段。第三个字段将自动填写。
【问题讨论】:
-
如果
rate和quantity已经分别是50和2但用户在Price中输入150怎么办?我们如何更新其他 2 列?将rate和quantity分别更新为50和3还是分别更新为3和50?还是15和10?你的问题其实有点不清楚。在Binding的上下文中,这甚至变得更加复杂。
标签: c# sql winforms datagridview