错误而试图编辑datagridview的

问题描述:

我有一个datagridview它有3列错误而试图编辑datagridview的

列1 =产品编号

列2 =价格

列3 =数量

我存在的行如果产品ID和价格相同,我试图将数量加1(+1)。并应增加新的行datagridview如果datagridview

不存在产品ID我的问题是 我在else if (!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; 
        } 
       } 
      } 
     } 

最后else部分将始终会增加,从而相同的记录被执行两次 所以我必须在else if (!ProductIDExist)部分和解决问题的末尾添加return ...

如果有任何代码改进像更好的方法来代码或其他任何东西