对象不包含'单元格的定义'错误

时间:2017-07-22 17:44:46

标签: c# object datagridview

我有两个datagridview。 datagridview1和datagridview2。当我将datagridview1中的产品添加到datagridview2时,datagridview1中的产品数量将传输到datagridview2。现在当我从datagridview2中删除一个产品时,我需要它将数量转移回datagridview1。

enter image description here

有人帮助我,但我现在得到这个错误。 enter image description here

    private int FindRowIndexByID(string id)
    {
        for (int i = 0; i < dgvPOSproduct.Rows.Count; i++)
        {
            if (dgvPOSproduct.Rows[i].Cells[0].Value.ToString() == id)
            {
                return i;
            }
        }

        return -1;
    }

    private void btnRemove_Click(object sender, EventArgs e)
    {

        foreach (var row in dgvPOScart.SelectedRows)
        {
            // Get the row in dgvProducts and the quantity he'll gain back
            var productRow = dgvPOSproduct.Rows[FindRowIndexByID(row.Cells[0].Value)];
            int qtyToAdd = Convert.ToInt32(row.Cells[4].Value);

            // Add the quantity back
            productRow.Cells[5].Value = Convert.ToInt32(productRow.Cells[5].Value) + qtyToAdd;
        }

    }

1 个答案:

答案 0 :(得分:1)

在for循环中更改var的实际类型,即DataGridViewRow。 像这样:
foreach (DataGridViewRowrow in dgvPOScart.SelectedRows){ ....