C#-在datagridview上计算总价

C#-在datagridview上计算总价,c#,C#,我创建了一种方法,可以检测何时输入了具有相同条形码的产品,而不是将其作为新行输入,它只会增加其数量。问题是,我有第四行,其中应该显示总价,但它不是 代码: 我在这里只是猜测,因为您的代码不是最干净的,但是您似乎只更新了网格中的数量。试试这个(也稍微干净一点) 找到条形码后,您没有调用计算方法。 喜欢 if (Convert.ToString(row.Cells[0].Value) == txtBarcode.Tex

我创建了一种方法,可以检测何时输入了具有相同条形码的产品,而不是将其作为新行输入,它只会增加其数量。问题是,我有第四行,其中应该显示总价,但它不是

代码:


我在这里只是猜测,因为您的代码不是最干净的,但是您似乎只更新了网格中的数量。试试这个(也稍微干净一点)


找到条形码后,您没有调用计算方法。 喜欢

                   if (Convert.ToString(row.Cells[0].Value) == 
                     txtBarcode.Text)
                         {
                          row.Cells[3].Value =Convert.ToString(1 + 
                           Convert.ToInt64(row.Cells[3].Value;

               price = float.Parse(txtPrice.Text);
               Quantity = float.Parse(Quantity.Text);
               FinalPrice = price * Quantity;

                       Found = true;
                //break;
                      }

你调试过你的代码吗?它没有显示任何错误,但是它没有用最后的价格更新第五行。在
Finle price
中返回什么?检查了吗?然后执行
row.Cells[4].Value=Convert.ToString(Convert.ToInt64(row.Cells[2].Value)*Convert.ToInt64(row.Cells[4].Value))在您中断之前。显然,在设置
Found=true之前,您只是在更新代码中的数量并要断开显示数量的第四行,请将其更改为最终价格。它也可能解决此问题。
var existingRow = dataGridView1.Rows
    .Where(r => Convert.ToString(r.Cells[0].Value) == txtBarcode.Text)
    .SingleOrDefault();

if(existingRow != null)
{
    var price = Convert.ToInt64(existingRow.Cells[2].Value);
    var quantity = Convert.ToInt64(existingRow.Cells[3].Value);

    existingRow.Cells[3].Value = quantity + 1;
    existingRow.Cells[4].Value = price * (quantity + 1);
}
else
{
    // Your !found logic
}
                   if (Convert.ToString(row.Cells[0].Value) == 
                     txtBarcode.Text)
                         {
                          row.Cells[3].Value =Convert.ToString(1 + 
                           Convert.ToInt64(row.Cells[3].Value;

               price = float.Parse(txtPrice.Text);
               Quantity = float.Parse(Quantity.Text);
               FinalPrice = price * Quantity;

                       Found = true;
                //break;
                      }