C# 如何计算datagridview列?
我是C# 如何计算datagridview列?,c#,C#,我是C#winform的新用户,也是这个网站的新用户 我有一个带有4列的datagridview No. proname price qty total 1 fish 0.0 1 - 2 tofu 0.0 1 - 示例中的数据是这样的,我给出了默认的价格,上面
C#winform
的新用户,也是这个网站的新用户
我有一个带有4列的datagridview
No. proname price qty total
1 fish 0.0 1 -
2 tofu 0.0 1 -
示例中的数据是这样的,我给出了默认的价格,上面的数量是空的。现在我需要cell单击列price上的
,在我给出新值后,我按Enter键将其保留,或者无论如何只保留单元格,我需要currentrow上的price*qty=total
。或相反,我新输入数量到2。。。而价格不是“0.0”,则price*qty=total
但如果我在另一个单元格上编辑,上面的两个事件就什么也不做了
我该如何乘以它
谁能帮我找到更好的解决方案
提前谢谢。在代码隐藏中使用此选项
int.Parse(row.Cells[3].Value.toString()) * int.Parse(row.Cells[4].Value.toString())
你的方法是这样的
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();
}
}
这正是您想要的 这个人给出的答案解释得很好,实现起来并不难 通过这种方式,“总计”列将通过 表达式,这意味着您不能手动对该列执行任何操作
您应该查看“数据绑定”,并拥有模型的一个属性
Total
,该属性返回基于价格和数量的总值属性WinForms或WPF?您使用的是数据表吗?您使用数据绑定吗?编辑问题。问题可能重复