2016-08-19 6 views
0

アイテムがすでにDataGridViewに存在する場合に問題があり、同じアイテムを再度入力すると、数量と合計が増減しません。同じ項目が表示されます。DataGridViewの既存のアイテムに数量を追加する方法

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

ようこそスタックオーバーフロー。あなたはあなたの質問を改善することができます。 [最小、完全、および検証可能な例](http://stackoverflow.com/help/mcve)をお読みください。あなたのコードが何も特別な問題であなたの正確な問題を示すとき、あなたはボランティアしてあなたを助ける人たちに敬意を表しています。 – zhon

+0

あなたのお手伝いをさせていただきます。 – zhon

+0

datagridviewの値そのものではなく、datagridviewの基になるデータソースを追加/更新する必要があります –

答えて

0

私は何をしてDataGridViewのをループし、そこにISN場合は、列に1を加えるがある場合、私は、挿入するつもり一致するレコードを探し行を挿入しないでください。

例:

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 

ホープこのことができます!

関連する問題