更新时间:2023-02-23 22:55:11
您正在浏览整个DataGridView,因此您可能只是拿起最后一个行,这是用户可以在哪里添加一行。
尝试单步执行代码来确认这一点,或者在循环开始时将val设置为一个奇怪的数字来证明这一点例如
You are going through the entire DataGridView so you are probably just picking up the last "Row" which is where the user can add a row.
Try stepping through your code to confirm this, or set val to a strange number at the start of the loop to prove the point e.g.
private void metroGrid1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
int val = -99;
foreach (DataGridViewRow row in metroGrid1.Rows)
{
val = Convert.ToInt32(row.Cells["quantity"].Value);
}
totPenTxt.Text = val.ToString();
}
通过阅读本文了解如何调试自己的代码 - 掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]