简体   繁体   English

更新 DataGridView 中的单元格,否则创建新行

[英]Update Cell in DataGridView Else Make New Row

I have two DataGridView Tables.我有两个 DataGridView 表。 One contains all the products and quantity available MO_CatData一个包含所有可用的产品和数量MO_CatData 在此处输入图片说明

The second is the items you add to the table.第二个是您添加到表格中的项目。 MO_quantityData 在此处输入图片说明

Now, if I choose a selected row from MO_CatData and click Add to Order it should loop through MO_quantityData .现在,如果我从MO_CatData选择一个选定的行并单击Add to Order它应该循环遍历MO_quantityData If the Product ID is already inside the table then we just update the quantity;如果产品 ID 已经在表中,那么我们只需更新数量; Else we make a new Row.否则我们创建一个新行。

So to get the product ID in the MO_CatData I've used:因此,要在我使用的MO_CatData获取产品 ID:

int id = Convert.ToInt32(MO_CatData.SelectedRows[0].Cells[0].Value);

Then I want to use a for loop and check whether it exists;然后我想使用for循环并检查它是否存在; if it does it'll update otherwise create a new row:如果它会更新,否则会创建一个新行:

else
{
    int id = Convert.ToInt32(MO_CatData.SelectedRows[0].Cells[0].Value);
    for (int i = 0; i < MO_quantityData.Rows.Count; i++)
    {
        Debug.WriteLine(id);

        if (Convert.ToInt32(MO_quantityData.Rows[i].Cells[2].Value) == id)
        {
            Debug.WriteLine("Gets to if");
            qty = Convert.ToInt32(MO_quantity.Text);
            int newQty = Convert.ToInt32(MO_quantityData.Rows[i].Cells[3].Value) + qty;
            table.Rows[i][3] = newQty;

            float totalprice = newQty * price;
            totalPrice = totalprice;
            sum += totalPrice;

            table.Rows[i][5] = totalPrice;
            MO_quantityData.DataSource = table;
            MO_CatData.Refresh();
        }
        else
        {
            Debug.WriteLine("ELESLESLELES");
            num = num + 1;

            qty = Convert.ToInt32(MO_quantity.Text);

            float totalprice = qty * price;
            totalPrice = totalprice;

            table.Rows.Add(num, product, prodID, qty, price, totalprice);
            MO_quantityData.DataSource = table;
            MO_CatData.Refresh();

            flag = 0;
            sum += totalPrice;
        }
    }
}

I feel as if my logic is incorrect somewhere because if I try to add 2 Pizza, then 1 Chicken then another pizza I get:我觉得我的逻辑在某处是不正确的,因为如果我尝试添加 2 个披萨,然后是 1 个鸡肉,然后是另一个披萨,我会得到:

在此处输入图片说明

Is using a for statement wrong in this scenario?在这种情况下使用 for 语句是否错误? I thought it would be just as simple as loop through the table, if the increment of the loop, at cell 2 is equal to the selectedRow then update quantity else make a new row but clearly not.我认为这就像遍历表格一样简单,如果循环的增量,在单元格 2 等于 selectedRow 然后更新数量否则创建一个新行,但显然不是。

If you break the code up a little, you will probably see what goes wrong: You need to stop the search when you find the right product.如果您将代码稍微分解一下,您可能会看到哪里出了问题:当您找到合适的产品时,您需要停止搜索。 I've added a FindRowWithId function that takes care of finding it, and the rest is left as is.我添加了一个FindRowWithId函数来处理它,其余的保持原样。 You would probably also benefit from only having the price calculations and so on in one place.您可能还会受益于仅在一个地方进行价格计算等。

private int FindRowWithId(int id)
{
    for (int i = 0; i < MO_quantityData.Rows.Count; i++)
    {
        if (Convert.ToInt32(MO_quantityData.Rows[i].Cells[2].Value) == id)
        {
            return i;
        }
    }
    return -1; // Not found;
}

...
else
{
    int id = Convert.ToInt32(MO_CatData.SelectedRows[0].Cells[0].Value);
    int foundRow = FindRowWithId(id);
    if (foundRow > -1)
    {
        Debug.WriteLine("Gets to if");
        qty = Convert.ToInt32(MO_quantity.Text);
        int newQty = Convert.ToInt32(MO_quantityData.Rows[foundRow].Cells[3].Value) + qty;
        table.Rows[foundRow][3] = newQty;
        float totalprice = newQty * price;
        totalPrice = totalprice;
        sum += totalPrice;

        table.Rows[foundRow][5] = totalPrice;
        MO_quantityData.DataSource = table;
        MO_CatData.Refresh();
    }
    else
    {
        Debug.WriteLine("ELESLESLELES");
        num = num + 1;

        qty = Convert.ToInt32(MO_quantity.Text);

        float totalprice = qty * price;
        totalPrice = totalprice;

        table.Rows.Add(num, product, prodID, qty, price, totalprice);
        MO_quantityData.DataSource = table;
        MO_CatData.Refresh();

        flag = 0;
        sum += totalPrice;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM