简体   繁体   English

添加带有循环的listview子项

[英]Adding listview subitems with loop

Help please. 请帮助。 Here's my table. 这是我的桌子。

 ID  Item        Qty   Added Qty

 1   Ballpen      23     5

 2   Pencil       44     4

 3   Pentelpen    12     5

I want to update the items once, but when I run my program it becomes the same as the first item. 我想一次更新项目,但是当我运行程序时,它与第一项相同。

For example when click the save button, all item qty will be 28 like ballpen. 例如,当单击“保存”按钮时,所有数量(如圆珠笔)将为28。

Here's my code. 这是我的代码。 Thanks in advance. 提前致谢。

                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

As @Blackwood said, you're only calculating qty once outside the loop and using the same value for each item. 正如@Blackwood所说,您只需要在循环外计算一次数量,并对每个项目使用相同的值。 You want to do it in the loop like this (and using more VB.NET style constructs makes code easier to read. This isn't VB6 :-) ) 您想像这样在循环中做到这一点(并且使用更多的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM