使用循环添加listview子项

时间:2017-10-09 13:51:04

标签: vb.net

请帮助。这是我的餐桌。

 ID  Item        Qty   Added Qty

 1   Ballpen      23     5

 2   Pencil       44     4

 3   Pentelpen    12     5

我想更新一次项目,但是当我运行我的程序时,它会与第一项相同。

例如,当单击“保存”按钮时,所有项目数量将为28,如圆珠笔。

这是我的代码。提前谢谢。

                Dim lvitem As Object
                Dim iCount As Integer
                Dim iLoop As Integer
                Dim qty As Double = Val(lvPO.Items(0).SubItems(1).Text) + Val(lvPO.Items(0).SubItems(2).Text)

                iCount = lvPO.Items.Count()
                If Not lvPO.Items.Count = 0 Then
                    Do Until iLoop = lvPO.Items.Count
                        lvitem = lvPO.Items.Item(iLoop)
                        With lvitem
                        Call SEDCommand("E", "tbl_item", "  qty = '" & qty & "' WHERE itemid ='" & .SubItems(0).text & "'")
                        End With
                        iLoop = iLoop + 1
                        lvitem = Nothing
                    Loop
                    MsgBox("Purchase Order (" & tb_refpo.Text & ") is added in database!", MsgBoxStyle.Information, "SYSTEM")
                    ClearTextBox(Me)
                End If

1 个答案:

答案 0 :(得分:1)

正如@Blackwood所说,你只在循环外计算一次数量,并为每个项目使用相同的值。你想在循环中这样做(并且使用更多的VB.NET样式构造使代码更容易阅读。这不是VB6 :-))

   If lvPO.Items.Count <> 0 Then
      For Each lvitem as ListViewItem in lvPO.Items
         With lvitem
            Dim qty As Double = Val(.SubItems(1).Text) + Val(.SubItems(2).Text)
            SEDCommand("E", "tbl_item", "  qty = '" & qty & "' WHERE itemid ='" & .SubItems(0).text & "'")
         End With
      Next
      MsgBox("Purchase Order (" & tb_refpo.Text & ") is added in database!", MsgBoxStyle.Information, "SYSTEM")
      ClearTextBox(Me)
   End If