首页 文章

如果项已存在,则将值添加到datagridview

提问于
浏览
0

嗨,大家好,我的问题 . 我有2个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());           
        }
    }

enter image description here

1 回答

  • 1

    试着改变

    dgvOrdercart.Rows[productcartID].Cells[3].Value = dgvnewquantity;
    

    row.Cells[3].Value = dgvnewquantity;
    

相关问题