如果项目已经存在,则将值添加到datagridview中
问题描述:
嗨,大家好,这里是我的问题。我有2个datagridview。如果项目已经存在,则将值添加到datagridview中
我想要做的是,每当我点击“这datagridview1行(产品)添加到datagridview2”,并从datagridview1该产品已经在datagridview2,它只会增加一个文本框(量)的值的数量细胞在datagridview2中具有与正在插入的ID相同的ID。
它可以检测到您插入到datagridview2的产品是否已经存在,但是我不能让它将该文本框的值添加到datagridview中的现有单元格。 这是我的代码(这是错误的,不工作)和datagridviews的图片。
private bool alreadyincart()
{
foreach (DataGridViewRow row in dgvOrdercart.Rows)
{
int productcartID = Convert.ToInt32(dgvOrderproductlist.CurrentRow.Cells[0].Value.ToString());
if (Convert.ToInt32(row.Cells[0].Value) == productcartID)
{
MessageBox.Show("Item already exist, Adding the quantity instead.");
int textboxquantity = Convert.ToInt32(bakss.Text);
int dgvquantity = Convert.ToInt32(row.Cells[3].Value);
int dgvnewquantity;
dgvnewquantity = dgvquantity + textboxquantity;
dgvOrdercart.Rows[productcartID].Cells[3].Value = dgvnewquantity;
return false;
}
}
return true;
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (!validate())
{
return;
}
else if (!alreadyincart())
{
return;
}
else
{
addData(dgvOrderproductlist.CurrentRow.Cells[0].Value.ToString(),
dgvOrderproductlist.CurrentRow.Cells[1].Value.ToString(),
dgvOrderproductlist.CurrentRow.Cells[2].Value.ToString(),
bakss.Text, lblPrice.Text, totalprayss.Text, cboOrderSupplier.SelectedItem.ToString());
}
}
你的逻辑是一种倒退,使其难以跟随你的代码。 '当购物车中出现**时,alreadyincart()'返回false,当找不到时为true。这与它应该是相反的。 – AgapwIesu
是的,我注意到很抱歉的混乱! – FutureDev