我创建了一种方法,该方法可以检测何时输入具有相同条形码的产品,而不是将其输入为新行,而只会增加其数量。问题是我有第四行显示总价格,但没有显示。
代码:
private void AddProduct()
{
bool Found = false;
if (dataGridView1.Rows.Count > 0)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
if (Convert.ToString(row.Cells[0].Value) == txtBarcode.Text)
{
row.Cells[3].Value =Convert.ToString(1 + Convert.ToInt64(row.Cells[3].Value));
Found = true;
//break;
}
}
if (!Found)
{
price = float.Parse(txtPrice.Text);
Quantity = float.Parse(Quantity.Text);
FinalPrice = price * Quantity;
var firstRow = txtBarcode.Text;
var secRow = txtName.Text;
var thirdRow = price;
var fourthrow = quantity;
var fifthrow = FinalPrice;
dataGridView1.Rows.Add(firstRow, secRow, thirdRow , fourthrow, fifthrow );
}
}
答案 0 :(得分:0)
我只是在这里猜测,因为您的代码不是最干净的,但是您似乎仅在更新网格中的数量。试试这个(也比较干净)
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
}
答案 1 :(得分:0)
找到条形码后,您不会调用计算方法。 喜欢。
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;
}