2016-08-19 67 views
0

當一個項目已經在DataGridView中,然後當再次輸入同一項目時,數量和總數不會增加或添加。它只會列出相同的項目。如何在DataGridView中的現有項目中添加數量 - vb.net

例如

Item Code ProductName Unit Item  Description Price Quantity Total Discount 
06-098  Biogesic  500mg Paracetamol    5.50 1  5.50 0.00 

TextBox輸入條形碼,它會在DataGridView列表中的項目。

這裏是我的代碼:

Private Sub txtbxBarcode_TextChanged(sender As Object, e As EventArgs) Handles txtbxBarcode.TextChanged 
    GetProductInfo() 
End Sub 

Private Sub GetProductInfo() 
    Dim discountAmount As Double 
    Dim medicineName, unit As String 
    Try 
     SQL = "SELECT product_code, Medicine_name, Unit, Description, Price, medicineID FROM medicine_info WHERE barcode = '" & txtbxBarcode.Text & "'" 
     ConnDB() 
     cmd = New MySqlCommand(SQL, conn) 
     dr = cmd.ExecuteReader 

     If dr.Read = True Then 
      txtbxItemCode.Text = dr("product_code") 
      unit = dr("Unit") 
      medicineName = dr("Medicine_name") 
      txtbxItemDesc.Text = dr("Description") 

      'Validate Discount 
      If isDiscount = True Then 
       discountAmount = Val(dr("Price")) * (Val(discountPercent)/100) 
       txtbxPrice.Text = Format(Val(dr("Price")) - discountAmount, "#,##0.00") 
      Else 
       txtbxPrice.Text = Format(dr("Price"), "#,##0.00") 
       discountAmount = 0 
      End If 
      'Validate Quantity 
      If isQuantity = True Then 
       txtbxQuantity.Text = noOfItems 
      Else 
       txtbxQuantity.Text = 1 
      End If 
      txtbxTotal.Text = Format(Val(txtbxPrice.Text.Replace(",", "")) * Val(txtbxQuantity.Text), "#,##0.00") 
      'Adding Item to Gridview to Display 
      dgv.Rows.Add(dr("medicineID"), dr("product_code"), dr("Medicine_name"), dr("Unit"), dr("Description"), txtbxPrice.Text, txtbxQuantity.Text, txtbxTotal.Text, Format(discountAmount * Val(txtbxQuantity.Text), "#,##0.00")) 
      'Get Basket Info 
      BasketInformation() 
      'Clear Barcode text field 
      txtbxBarcode.Clear() 
      'Set Discount to Zero 
      discountPercent = 0 
      isDiscount = False 
      'Set Quantity to False 
      isQuantity = False 
      noOfItems = 1 
     End If 
    Catch ex As Exception 
     MsgBox(ex.ToString) 
    Finally 
     cmd.Dispose() 
     conn.Close() 
    End Try 
End Sub 
+0

歡迎來到Stack Overflow。你可以改善你的問題。請閱讀[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。當你的代碼沒有任何額外的東西顯示你的確切問題時,你會向志願幫助你的人表示敬意。 – zhon

+0

你可以幫助我們通過格式化來幫助你,所以我們沒有滾動它。 – zhon

+0

您應該添加/更新datagridview的基礎數據源,而不是datagridview本身 –

回答

0

我要做的就是遍歷datagridview的並查找匹配的記錄我要去插入,如果有添加+1列,如果有ISN不插入該行。

例子:

For each row as DataGridViewRow in datagridview1.Rows 
    If row.Cells(0).Value = dr("ID") Then 
     row.Cells(6).Value = Integer.Parse(row.Cells(6).Value) + 1 
     Exit Sub 'Exit Sub for the purpose of not triggering the row add code since there is an existing record already. 
    End IF 
Next 

datagridview1.rows.add(....) 
0

你應該用數據將其放入一個DataGridView前處理這個問題。然後,當您擁有正確的數據時,您可以將其設置爲數據源。

此外,您需要使用數據表,而不是逐行讀取記錄。填充數據表看起來像這樣..

Dim myTable = new DataTable 
Using adapter = New SqlDataAdapter(cmd) 
    adapter.Fill(myTable) 
End Using 

和編輯數據表看起來像這樣..

For x as Integer = myTable.rows.Count - 1 to 1 
    For y as Integer = x - 1 to 0 
     If myTable.rows(x).Item("Item Code") = myTable.rows(y).Item("Item Code") Then 
      myTable.rows(x).Item("Quantity") = myTable.rows(x).Item("Quantity") + 1 
      myTable.rows(x).Item("Total") = myTable.rows(x).Item("Total") + myTable.rows(y).item("Total") 
      myTable.rows(y).Delete() 
     End If 
    Next 
Next 

myTable.acceptChanges() 
dgv.dataSource = myTable 'this will put all rows from myTable into the dgv 

希望這有助於!

相關問題