我是一名正在开发的新学生,我正在寻找可以帮助我在 datagridview 中的行总和时更改标签的 C# 代码
例如:我正在使用此代码来计算 datagridview 中的特定行
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[dataGridView1.Columns["Amount"].Index].Value =
(Convert.ToDecimal(row.Cells[dataGridView1.Columns["Qtty"].Index].Value) *
Convert.ToDecimal(row.Cells[dataGridView1.Columns["PurchasePrice"].Index].Value));
}
我想要做的是:在我填写PurchasePrice并qtty计算金额之后,当我按下回车按钮时,标签应该是 = 中的相同金额datagridview。
谢谢
编辑:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.Cells[dataGridView1.Columns["Amount"].Index].Value = (Convert.ToDecimal(row.Cells[dataGridView1.Columns["Qtty"].Index].Value) * Convert.ToDecimal(row.Cells[dataGridView1.Columns["PurchasePrice"].Index].Value));
}
//datagridview column total in label
decimal sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
sum += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
}
lblSum.Text = sum.ToString();
//vat
decimal vat = (sum * 15 / 100);
lblVat.Text = vat.ToString();
//subtotal
decimal subtotal = (sum - vat);
lblSubtotal.Text = subtotal.ToString();
}