3

I need to dynamically select an item in a listview based on what was selected previously.

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.

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.

redned
  • 301
  • 1
  • 3
  • 21

4 Answers4

8

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
Tom F
  • 807
  • 6
  • 20
1

You can try 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()
    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

And 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
matzone
  • 5,703
  • 3
  • 17
  • 20
  • Iterating through both the list view and the array and comparing each did the trick, but this is a nice solution. Thanks! – redned May 16 '13 at 21:10
0

The code to select an item dynamically from the listview control can be as follows for vb.net.

  • Let lvwomominiChair1 is the name of the listview control.
  • Set its fullrowselect property as true.

The code will select the text in the first column of the listview control.

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

There may be situations where we need to get all items in a row of a ListView control.The following code may be used for the purpose.It is assumed that there are five columns of data in a raw and are of the text data type.This can be done with a For..Next loop as follows.Let 0,1,2,3 and 4 are the five column indices.

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
0

Or you can do this, works perfect for me:

ListView.Items(0).Selected = True

ListView.Select()
ytsejam
  • 3,291
  • 7
  • 39
  • 69
Daniel
  • 61
  • 5