我需要根据之前选择的内容动态选择列表视图中的项目。
从数据库中检索过去选择的项目并将其添加到Arraylist中。然后,需要从许多不同的列表视图中选择这些项目。
通过像listRef1.Items(2).Checked = True
这样的索引来做这个没问题但是我需要通过项目文本来做,即数组中的一个字符串。
到目前为止,我有这个:
For i As Integer = 0 To refsArr.Count - 1
'find the correct category id
Dim cmdRefCat As New SqlCommand("SELECT RefID from ReferencesListTable WHERE RefName = '" & refsArr(i) & "'", conn)
Dim refid As Integer = cmdRefCat.ExecuteScalar()
If refid = 1 Then
listRef1.Items(refsArr(i)).Checked = True
ElseIf refid = 2 Then
listRef2.Items(refsArr(i)).Selected = True
listRef2.Select()
ElseIf refid = 3 Then
listRef3.Items.Item(refsArr(i)).Selected = True
listRef2.Select()
ElseIf refid = 4 Then
listRef4.Items.Item(refsArr(i)).Selected = True
End If
Next
有没有人对此有任何想法?感谢。
答案 0 :(得分:6)
您需要遍历列表视图列表中的每个项目:
For I as Integer = 0 to ListView.Items.Count - 1 Do
If ListView.Items(i).Text = "Text" then
ListView.Items(i).Selected = true
End If
End For
答案 1 :(得分:1)
你可以试试这个......
For i As Integer = 0 To refsArr.Count - 1
'find the correct category id
Dim cmdRefCat As New SqlCommand("SELECT RefID from ReferencesListTable WHERE RefName = '" & refsArr(i) & "'", conn)
Dim refid As Integer = cmdRefCat.ExecuteScalar()
Select case refid
case 1
CheckIt(refsArr(i),listRef1)
case 2
CheckIt(refsArr(i),listRef2)
case 3
CheckIt(refsArr(i),listRef3)
case 4
CheckIt(refsArr(i),listRef4)
End Select
Next
和Sub CheckIt
Sub CheckIt(ByVal sRef as String, ByRef lvw as Listview)
Dim x as Integer
For x = 0 to lvw.Items.Count - 1
If lvw.Items(x).Text = sRef then
lvw.Items(x).Selected = true
exit for '-- if only 1 record
End If
Next
End Sub
答案 2 :(得分:0)
从listview控件动态选择项目的代码对于vb.net可以如下。
lvwomominiChair1
是listview控件的名称。代码将选择listview控件第一列中的文本。
Private Sub lvwomominiChair1_Click(sender As Object,e As EventArgs) Handles lvwomominiChair1.Click
Dim lvwitem as ListViewItem
lvwitem = lvwomominiChair1.SelectedItems.Item(0)
MsgBox("Selected item is " + lvwitem.Text)
End Sub
在某些情况下,我们需要获取ListView控件的一行中的所有项目。以下代码可用于此目的。假设原始数据中有五列数据且属于文本数据类型。这可以通过For..Next循环完成,如下所示。让0,1,2,3和4是五列索引。
Private Sub lvwomominiChair1_Click(sender As Object,e As EventArgs) Handles lvwomominiChair1.Click
Dim i As Int32
Dim str As String
str =""
For i =0 To 4
str = str + " " + lvwomominiChair1.SelectedItems(0).SubItems(i).Text
Next
MsgBox("Selected items of the five columns of the row are " + str)
End Sub
答案 3 :(得分:0)
或者你可以做到这一点,对我来说非常适合:
ListView.Items(0).Selected = True
ListView.Select()