我正在做一个学校项目,并且陷入这种情况的时间已经有几天了。我找不到在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中。
答案 0 :(得分:1)
您必须遍历Listview
中的已选中项目,而不是Datatable。您的代码可能类似于:
For Each item As ListViewItem In YourListView.CheckedItems
totalfees += CDbl( item.SubItems("fee_price").Text)
Next