Oracle 尝试编辑datagridview中的exist行时出错

Oracle 尝试编辑datagridview中的exist行时出错,oracle,c#-4.0,datagridview,Oracle,C# 4.0,Datagridview,我有一个datagridview它有3列 第1列=产品ID 第2列=价格 第3列=数量 如果重复使用相同价格的产品id,我尝试将数量增加1(+1)。如果产品id不存在于datagridview 我的问题是 如果(!ProductIDExist)中显示 索引超出范围。必须为非负数且小于 收藏 最后一个else部分将在always中执行,因此相同的记录将添加两次 因此,我在的末尾添加了return,如果(!ProductIDExist)部分和问题已解决 如果有任何代码改进,比如更好的编写代码的方法或

我有一个
datagridview
它有3列

第1列=产品ID

第2列=价格

第3列=数量

如果重复使用相同价格的产品id,我尝试将数量增加1(+1)。如果产品id不存在于
datagridview

我的问题是 如果(!ProductIDExist)中显示

索引超出范围。必须为非负数且小于 收藏


最后一个
else
部分将在always中执行,因此相同的记录将添加两次 因此,我在
的末尾添加了
return
,如果(!ProductIDExist)
部分和问题已解决

如果有任何代码改进,比如更好的编写代码的方法或者其他什么

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";

            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.Int64)).Value = TB_Product_ID.Text;

                OracleDataReader oraReader = cmd.ExecuteReader();

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

                    DataGridViewRow dgvRow = new DataGridViewRow();

                    if (DGV_INVOICE.Rows.Count > 0)
                    {
                        foreach (DataGridViewRow ItemRow in DGV_INVOICE.Rows)
                        {
                            //Check if the product Id exists with the same Price
                            if (Convert.ToString(ItemRow.Cells[2].Value) == TB_Product_ID.Text)
                            {
                                //Update the Quantity of the found row
                                ItemRow.Cells[5].Value = Convert.ToString(1 + Convert.ToInt64(ItemRow.Cells[5].Value));
                                ProductIDExist = true;
                            }
                            else if (!ProductIDExist)
                            {
                                //Add the row to grid view

                                //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;
                            }
                        }
                    }
                    else
                    {
                        //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;
                    }
                }
            }
        }