Hi all, I really need your help on this one. I have a MSSQL db table with two columns-ID and name. Each row in this two columns contains an unique ID and a name.

Now I have a query that imports just a name in the listview (I don't want ID value in listview). I've put all the values from "ID" column in an array.

Now what I want to do is that when listview item is clicked, the program recongnizes listview selected item index and gets a unique ID (I will need this ID to structure another sql query, but this is not the thing now). So program should recognize list view selected item index and pull out a value from an array which has the same row number as that index.

this is my code so far:

Dim listviewindex As New List(Of Integer)

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

        Dim ConnString As String
        Dim SQLStr As String


        ConnString = "Data Source=xyz;Initial Catalog=xyz;UID=xyz;Pwd=xyz"
        SQLStr = "select * from db.dbo.xyz order by NAME asc"

        Dim SQLConn As New SqlConnection()
        Dim SQLCmd As New SqlCommand()
        Dim dr As SqlDataReader

        SQLConn.ConnectionString = ConnString
        SQLConn.Open()

        SQLCmd.Connection = SQLConn
        SQLCmd.CommandText = SQLStr
        dr = SQLCmd.ExecuteReader

        While dr.Read()
            ListView1.Items.Add(dr.Item("NAME"))
            listviewindex.Add((dr.Item("ID")))

        End While

        Dim x As Integer
        x = ListView1.FocusedItem.Index
        MsgBox(x)

        SQLConn.Close()
        SQLConn.Dispose()

    End Sub

What's bugging me first that when I click an item in listview, msgbox(x) gives me correct value on first click, but on second it first gives me index value from previous clicked item and only then it gives me current clicked item index. How can I reset this "click" and how can I get the appropriate ID value from an array.

tnx a lot

Now, it's been a while since I've worked with VB & form controls, but it sure looks like you are repopulating your listview every time you change selections. Seems to me you should have an "onload" or similar function to populate the listview, and the "selectedindexchanged" handler should only handle that operation on the previously created listview. That might fix your problem.

One quick and easy way to do this, is to load the "ID" as a subitem of the listviewitem. Setting View to 'List', or 'Details' with one column will only show the item, but the subitem is still there and you can access it from the SelectedItem property. In the SelectedIndexChanged event handler you would do something like this:

Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
    If ListView1.SelectedItems.Count > 0 Then
        MessageBox.Show(ListView1.SelectedItems(0).SubItems(1).Text)
    End If
End Sub

To load the listview you could do something like this:

ListView1.Items.Add(dr.Item("NAME")).SubItems.Add(dr.Item("ID"))

Now data in each pair is tied together

Tinstaafl thats exactlly what I've needed! Thanks a lot! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.