VB.NET:如何动态选择列表视图项? [英] VB.NET: How to dynamically select a list view item?
问题描述
我需要根据先前选择的内容在列表视图中动态选择一个项目。
I need to dynamically select an item in a listview based on what was selected previously.
从数据库中检索过去选择的项目,然后添加到Arraylist。然后需要从许多不同的列表视图中选择这些项目。
The items that have been selected in the past are retrieved from a database and added to an Arraylist. These items then need to be selected from a number of different listviews.
按索引执行操作,例如 listRef1.Items(2).Checked = True
没问题,但是我需要通过项目文本(即数组中的字符串之一)来完成。
Doing this by index like so listRef1.Items(2).Checked = True
is no problem but I need to do it by the item text, i.e. one of the strings in the array.
到目前为止,我有这个:
So far I have this:
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
有人对此有任何想法吗?谢谢。
Has anyone got any ideas on this? Thanks.
推荐答案
您需要遍历列表视图列表中的每个项目:
You'll need to loop through each item in the listview list:
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
这篇关于VB.NET:如何动态选择列表视图项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!