首页 > 解决方案 > 如何添加我在 ListView CheckBox 中选中的项目的值

问题描述

我正在做一个学校项目,并且发现自己在这种情况下困了多少天了。我找不到在 ListView 中添加选中项的值的方法。

我找到了一种在不检查 CheckBox 的情况下添加它们的方法,但是如果我检查了 CheckBox 中的项目,我正在寻找一种添加它们的方法。

Module PaymentControllerModule
    Public Sub TotalFeeValue()
        Dim query As String = "SELECT * FROM tbl_fees"
        Dim totalfees As Double

        Dim adapter As New MySqlDataAdapter
        Dim command As New MySqlCommand
        Dim table As New DataTable
        Dim i As Integer
        Dim o As Integer

        With command
            .CommandText = query
            .Connection = databaseconnect
        End With

        With adapter
            .SelectCommand = command
            .Fill(table)
        End With

        formPaymentsDynamic.Lv_Paymentfees.Items.Clear()

        For i = 0 To table.Rows.Count - 1
            With formPaymentsDynamic.Lv_Paymentfees
                .Items.Add(table.Rows(i)("fee_description"))
                With .Items(.Items.Count - 1).SubItems
                    .Add(table.Rows(i)("fee_price"))
                End With
            End With

            totalfees += Val(table.Rows(i)("fee_price"))
            formPaymentsDynamic.txtTotalAmount.Text = totalfees
        Next
    End Sub
End Module

我期望在 ListView 中检查的总和项目的输出,但即使不检查它们,我也只能做所有项目的总和值。我希望你能帮助我。

这是程序的示例输出,即使没有检查,我也设法在 ListView 中添加值。

形式

标签: vb.net

解决方案


您必须遍历Listview而不是数据表中的已检查项目。您的代码可能类似于:

For Each item As ListViewItem In YourListView.CheckedItems
    totalfees  += CDbl( item.SubItems("fee_price").Text)
Next

推荐阅读