【问题标题】:How to delete removed items of datagridview from database after button click event按钮单击事件后如何从数据库中删除已删除的datagridview项目
【发布时间】:2016-03-20 20:34:22
【问题描述】:

我的表单中有DataGridView 并保存Button。我正在将购买的物品从TextBoxes 添加到DataGridView 并单击保存Button 后保存到数据库。这是相同的代码 -

        foreach (DataGridViewRow row in dgvOrderList.Rows)
        {
            if ((bool)row.Cells[0].Value)
            {
                int productId = Convert.ToInt32(row.Cells["clmCode"].Value);
                int quantity = Convert.ToInt32(row.Cells["clmQty"].Value);
                int amount = Convert.ToInt32(row.Cells["clmAmount"].Value);

                if (_orderBLL.CreateOrderedProduct(productId, quantity, amount, orderId))
                {
                    status = true;
                    row.Cells[0].Value = false;
                }
                else
                    status = false;
            }
        }

我还有一个名为 Remove 的 Button 用于从购买的商品列表中删除商品。这是相同的代码 -

            foreach (DataGridViewRow row in dgvOrderList.Rows)
            {
                if (row.Selected)
                {
                    dgvOrderList.Rows.RemoveAt(row.Index);
                    int div = Convert.ToInt32(row.Cells[5].Value);

                    total = total - div;
                    txtTotalAmount.Text = total.ToString();
                    if (!(bool)row.Cells[0].Value)
                    {
                        int productId = Convert.ToInt32(row.Cells[1].Value);
                        int orderId = Convert.ToInt32(txtOrderId.Text);
                        if(_orderBLL.IsOrderedProductExists(productId, orderId))
                        {
                            _orderBLL.DeleteOrderedProducts(productId, orderId);
                        }
                    }
                }
            }

但问题是,当我单击删除 Button 时,它会立即从 DataGridView 以及数据库中删除项目,而不是等待保存。

我的问题,有什么方法可以让我只能通过单击删除ButtonDataGridView 中删除项目,然后在单击保存Button 后从数据库中删除?

【问题讨论】:

  • 为什么不获取所选行.. 找到 KeyId 并创建一个方法,从数据库中删除 ProductID = 到所选行 productId 或 OrderId,使用调试器进行步骤通过代码查看您是否传递了DeleteOrderedProducts 方法的预期值
  • 而不是从删除按钮调用_orderBLL.DeleteOrderedProducts(productId, orderId);,您应该在保存方法中为任何已删除的项目执行此操作。这意味着当您从网格中删除项目时,您需要记住已删除的项目(例如在列表中)并在保存时访问它们。
  • @MethodMan 我的问题是在从DataGridView 中删除项目后控制如何在保存时从数据库中删除这些项目.. 如果我不能使用获取选定行方法来删除项目DataGridView
  • @OhBeWise.. 感谢您的建议.. 我会尝试并让您知道结果
  • @OhBeWise...我尝试了与您建议的完全相同的方法,并且效果很好...谢谢!

标签: c# winforms datagridview


【解决方案1】:

根据上述cmets中的建议,以下是上述问题的解决方案。

删除 Button 中的更改。添加整数数组pids[] 来存储将从DataGridView 中删除的产品ID,然后在保存订单并从数据库中删除时进行检查。

            foreach (DataGridViewRow row in dgvOrderList.Rows)
            {
                if (row.Selected)
                {
                    dgvOrderList.Rows.RemoveAt(row.Index);
                    //break; //use break if you only select one row! if selected more, then remove it!
                    pids[i] = Convert.ToInt32(row.Cells[1].Value);
                    i++;

                    int div = Convert.ToInt32(row.Cells[5].Value);

                    total = Convert.ToInt32(txtTotalAmount.Text);
                    total = total - div;
                    txtTotalAmount.Text = total.ToString();
                }
            }

更新了保存Button下面的代码

                foreach (int p in pids)
                {
                    if (p != 0)
                        _orderBLL.DeleteOrderedProducts(p, orderId);
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 2012-05-23
    相关资源
    最近更新 更多