Read ListView Items by Column Name

Hi, We use listView in our vb.net project (VB 2005). up until now we've been Referencing the item and sub item by index.  We'd like to change that and Reference item by it's column name.
for example:
it we have four columns in our list. i
Column1, Column2,Column3,Column4

current Code
        For i As Integer = 0 To listView1.Items.Count - 1
            Console.WriteLine(listView1.Items(i).SubItems(1).Text)
        Next

We'd like to use listView1.Items(i).SubItems("Column1").Text

I can't figure out how to do this currently, because it's not working right now.
vb7guyAsked:
Who is Participating?
 
strickddConnect With a Mentor Commented:
What is the issue with VB.net? My guess is it is a syntax change that needs to be made:

listView1.Columns("NameKey").Index
0
 
strickddCommented:
0
 
vb7guyAuthor Commented:
Thanks for the response. but this post doesn't work with VB.net
0
 
vb7guyAuthor Commented:
Here, I figured it out on my own. too bad, this isn't well documented in books online.

Console.WriteLine(listView1.Items(i).SubItems(listView1.Columns.IndexOfKey("column1")).Text)
0
 
vb7guyAuthor Commented:
Since your response was kind of partial. I'll accept it as the solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.