2017-02-22 50 views
0

我有一個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; 
        } 
       } 
      } 
     } 

回答

0

最後else部分將始終會增加,從而相同的記錄被執行兩次 所以我必須在else if (!ProductIDExist)部分和解決問題的末尾添加return ...

如果有任何代碼改進像更好的方法來代碼或其他任何東西