我的应用程序是一种商店的收银机,因此在数据网格中有引用,它是产品的引用。
当用户直接输入引用值时,有关产品的信息将填写在其他列中,用户可以选择他想要的数量并进行计算。
总和已完成,但如果我单击另一行,则会出现此错误:System.InvalidCastException
错误出现在 sum += Convert.ToInt32(TAB_Credit.Rows[i].Cells["Total"].Value);
这是评论:{"Unable to cast a DBNull object in other types."}
另外,我尝试将账单总额放入数据网格中......
Reference | Product |Quantity |Price |Total |Total Bill
AB |AB |2 |1000 |2000 |5000 <--my task is to find this
BC |BC |3 |1000 |3000
private void TAB_Credit_RowValidated(object sender, DataGridViewCellEventArgs e)
{
int sum = 0;
for (int i = 0; i < TAB_Credit.Rows.Count; ++i)
{
sum += Convert.ToInt32(TAB_Credit.Rows[i].Cells["Total"].Value);
}
TAB_Credit.Rows[e.RowIndex].Cells["Total_Bill"].Value = sum.ToString();
}
最佳答案
放置
TAB_Credit.Rows[TAB_Credit.Rows.Count - 1].Cells["Total_Bill"].Value = sum.ToString();
在 RowValidated
事件内。
关于c# - 如何在 C# 中计算每个新行的所有行的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43707342/