c# - 如果当前数据 GridView 中存在产品 ID,则将数量增加一 (+1)

标签 c# oracle datagridview

如果产品 ID 存在于当前数据 GridView 中,则将数量增加一 (+1)

我的数据 GridView 有 9 列:

Column 1 = Edit_Checkbox            Column 2 = ItemCount

Column 3 = DGV_PRODUCT_ID           Column 4 = DGV_PRODUCT_DESC

Column 5 = DGV_UNIT_PRICE           Column 6 = DGV_QUANTITY

Column 7 = DGV_DISCOUNT             Column 8 = DGV_TOTAL_PRICE

Column 9 = DGV_NOTES

如果当前数据 GridView 中存在产品 ID,我将尝试将数量增加一 (+1)

我下面的代码工作正常,但我有一个问题,当存在相同的产品 ID 时,它会 如果过程按顺序完成,则将数量增加一个,但是如果我输入产品 ID 以非顺序方式,它不会将数量增加一个,而是在 datagridview 中插入一个新行。

例如:

输入产品编号 1003 2 件数量为 2

输入产品编号 3000 1 件商品数量为 1

Enter product id number 1003 1 item quantity 当产品已经存在于datagridview中时仍为1。我该如何避免这种情况?

private void SelectedProductData()
        {
            int ItemCount = DGV_INVOICE.Rows.Count;
            bool ProductIDExist = false;

            string connstr = @"Data Source=orcl; User Id=user; password=pwd;";
            string cmdtxt = @"SELECT PRODUCT_ID,
                                     PRODUCT_DESC,
                                     UNIT_PRICE
                              FROM WAREHOUSE
                                WHERE PRODUCT_ID = :P_Product_ID";

            try
            {
                using (OracleConnection conn = new OracleConnection(connstr))
                using (OracleCommand cmd = new OracleCommand(cmdtxt, conn))
                {
                    conn.Open();

                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = cmdtxt;

                    cmd.Parameters.Add(new OracleParameter(":P_Product_ID", OracleDbType.Varchar2)).Value = TB_Product_ID.Text;

                    OracleDataReader oraReader = cmd.ExecuteReader();

                    while (oraReader.Read())
                    {
                        ItemCount++;
                        RowCountLabel.Text = ItemCount.ToString();

                        DataGridViewRow dgvRow = new DataGridViewRow();

                        foreach (DataGridViewRow ItemRow in DGV_INVOICE.Rows)
                        {
                          if (Convert.ToString(ItemRow.Cells[2].Value) == TB_Product_ID.Text)
                            {
                                MessageBox.Show(Convert.ToString(ItemRow.Cells[2].Value) + "  1  " + TB_Product_ID.Text);
                                ProductIDExist = true;
                                break;
                            }
                        }
                        if (ProductIDExist)
                        {
                            MessageBox.Show("2");
                            //dgvRow.Cells[5].Value = Convert.ToString(1 + Convert.ToInt64(dgvRow.Cells[5].Value));
                            DGV_INVOICE.Rows[dgvRow.Index].Cells[5].Value = Convert.ToString(1 + Convert.ToInt64(dgvRow.Cells[5].Value));
                        }
                        else
                        {
                            MessageBox.Show("3");

                            //Add the row to grid view for the first time
                            dgvRow.Cells.Add(new DataGridViewCheckBoxCell());   //Edit_Checkbox     index 0
                            dgvRow.Cells[0].Value = false;

                            dgvRow.Cells.Add(new DataGridViewTextBoxCell());    //ItemCount         index 1
                            dgvRow.Cells[1].Value = ItemCount;

                            dgvRow.Cells.Add(new DataGridViewTextBoxCell());    //DGV_PRODUCT_ID    index 2
                            dgvRow.Cells[2].Value = oraReader.GetValue(0);

                            dgvRow.Cells.Add(new DataGridViewTextBoxCell());    //DGV_PRODUCT_DESC  index 3
                            dgvRow.Cells[3].Value = oraReader.GetString(1);

                            dgvRow.Cells.Add(new DataGridViewTextBoxCell());    //DGV_UNIT_PRICE    index 4
                            dgvRow.Cells[4].Value = oraReader.GetValue(2);

                            dgvRow.Cells.Add(new DataGridViewTextBoxCell());    //DGV_QUANTITY      index 5
                            dgvRow.Cells[5].Value = "1";

                            dgvRow.Cells.Add(new DataGridViewTextBoxCell());    //DGV_DISCOUNT      index 6
                            dgvRow.Cells[6].Value = "0";

                            //dgvRow.Cells.Add(new DataGridViewTextBoxCell());  //DGV_TOTAL_PRICE   index 7
                            //dgvRow.Cells[7].Value = "0";

                            //dgvRow.Cells.Add(new DataGridViewTextBoxCell());  //DGV_NOTES         index 8
                            //dgvRow.Cells[8].Value = "-";

                            DGV_INVOICE.Rows.Add(dgvRow);
                            dgvRow.Selected = true;
                        }
                    }
                }
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                TB_ORDER_NOTE.Text = EX.Message;            /* Testing Purpose */
            }
        }

最佳答案

相反,尝试

bool ProductIDExist = false;
foreach (DataGridViewRow ItemRow in DGV_INVOICE.Rows)
{
    //Check if the product Id exists with the same Price
    ProductIDExist |= Convert.ToString(ItemRow.Cells[1].Value) == TB_Product_ID.Text

    if (productIDExist)
    {
        break;
    }
}

if (productIDExist)
{
    //increase quantity by 1
}
else
{
    //add a new row
}

关于c# - 如果当前数据 GridView 中存在产品 ID,则将数量增加一 (+1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42400357/

相关文章:

c# - 确定特定 DataGridView 单元格中的文本是否换行

vb.net - 如何使用 vb.net 将无限量的数据文件中的数据导入到 DataGridView 中?

c# - ASP.NET 相当于 JSP 包含

oracle - Glogin.sql 用于可插入数据库或 Oracle 上的不同数据库

c# - 如何让datagridview在离开一行时自动保存记录

oracle - 如何使用参数 'table of foo' 执行 SP?

oracle - 程序在包体中列出但未在规范中列出

c# - 是否有必要在winform控件上调用dispose?

c# - 无法将 lambda 表达式转换为类型 'bool' 因为它不是委托(delegate)类型?

c# - 具有简单进样器和Fluent验证的通用协方差