Sum calculation method

When working on a project, I will encounter some calculation methods, such as unit price plus quantity, or re-calculation of the unit price or quantity after re-modification. There are two methods in my project, the first is the code obtained online :
Decimal decQuantityTotal = 0.00m;
decimal decAmountTotal = 0.00m;
private void dgvStock_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{ if (dgvStock.Rows.Count> 0 && e.RowIndex >= 0) { if (dgvStock.Rows[e. RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == “Price” || dgvStock.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == “Quantity”) { try { dgvStock. Rows[e.RowIndex].Cells["Amount"].Value = Convert.ToDecimal(dgvStock.Rows[e.RowIndex].Cells["Quantity"].Value) * Convert.ToDecimal(dgvStock.Rows[e.RowIndex ].Cells[“Price”].Value); //txtFNo.Text = dt.Rows.Count.ToString();








//After the quantity or unit price is changed, recalculate the total quantity and total amount
for (int i = 0; i <dgvStock.Rows.Count; i++)
{ //total count try { decQuantityTotal += Convert.ToDecimal(dgvStock.Rows [i].Cells["Quantity"].Value); } catch {} //total amount of statistics try { decAmountTotal += Convert.ToDecimal(dgvStock.Rows[i].Cells["Price"].Value); } catch {} } lblQuantityTotal.Text = decQuantityTotal.ToString(); lblAmountTotal.Text = decAmountTotal.ToString(); } catch {} } } } The second one is modified: private void dgvStock_CellValueChanged(object sender, DataGridViewCellEventArgs e) {























if (dgvStock.Rows.Count > 0 && e.RowIndex >= 0)
{
if (dgvStock.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == “Price” || dgvStock.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name == “Quantity”)
{
try
{
dgvStock.Rows[e.RowIndex].Cells[“Amount”].Value = Convert.ToDecimal(dgvStock.Rows[e.RowIndex].Cells[“Quantity”].Value) * Convert.ToDecimal(dgvStock.Rows[e.RowIndex].Cells[“Price”].Value);
//txtFNo.Text = dt.Rows.Count.ToString();
}
catch { }
}
decQuantityTotal = 0.00m;
decAmountTotal = 0.00m;
for (int i = 0; i < dgvStock.Rows.Count; i++)
{
//统计数量合计
try
{
decQuantityTotal += Convert.ToDecimal(dgvStock.Rows[i].Cells[“Quantity”].Value);
}
catch { }
//统计金额合计
try
{
decAmountTotal += Convert.ToDecimal(dgvStock.Rows[i].Cells[“Amount”].Value);
}
catch { }
}
lblQuantityTotal.Text = decQuantityTotal.ToString();
lblAmountTotal.Text = decAmountTotal.ToString();
}
}

Guess you like

Origin blog.csdn.net/weixin_44540773/article/details/112873600