• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

ListView control

I have a Listview control, with multiple columns.  Every time I add a column, I have to go back and change my code to referenced the changed column ordinal values.

I access the ListView columns by ordinal value. Is there a way to access them by name?


Here is my code where I populate the listview:

        Dim li As New ListViewItem
        li.SubItems(0).Text = objDropdownQuery.order_to_run
        li.SubItems.Add(objDropdownQuery.category)
        li.SubItems.Add(objDropdownQuery.active)
        lvwMain.Items.Add(li)



Here is where I access the listview:

        li.SubItems(4).Text = "no table to drop"
0
brokeMyLegBiking
Asked:
brokeMyLegBiking
1 Solution
 
RonaldBiemansCommented:
Not that I know off :-)

you could add the listview items to a collection (but that seems like a lot of overkill to me)
0
 
gangwischCommented:
normally to reference them by name i would use a hashtable
for example:
dim mynames as new hashtable()
mynames.add("myname",li.SubItems(0).Text)
mynames.add("myname2",li.SubItems(1).Text)

'then to reference you would
mynames.item("myname")="Hello World" ' subitems(0).text changes to Hello World
0
 
RonaldBiemansCommented:
Hi Gangwisch, your suggestion won't work, since it won't change the actual listitem.

Your suggestion of using a hashtable or a collection like I suggested is the correct way though.

But like I said it is a lot of overkill. Because you will have store each item and each subitem in the hashtable and directcast back to the original type, like

       Dim mynames As New Hashtable
        ListView1.View = View.Details
        ListView1.Columns.Add("col1", 50, HorizontalAlignment.Center)
        ListView1.Columns.Add("col2", 50, HorizontalAlignment.Center)

        For x As Integer = 0 To 10
            Dim li As New ListViewItem
            Dim li2 As ListViewItem.ListViewSubItem
            li.Text = "test" & x
            li2 = li.SubItems.Add("s" & x)
            ListView1.Items.Add(li)
            mynames.Add("item" & x, li)
            mynames.Add("SubItem" & x, li2)
        Next
        DirectCast(mynames("item2"), ListViewItem).Text = "testing"
        DirectCast(mynames("SubItem1"), ListViewItem.ListViewSubItem).Text = "testing sub"

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
wguerramCommented:
Here is a function to get a column header by its text:

Public Function GetColumnHeaderByName(ByVal [ListView] As ListView, ByVal [Text] As String) As ColumnHeader
        Dim c As ColumnHeader

        For i As Integer = 0 To [ListView].Columns.Count - 1
            If [ListView].Columns(i).Text = [Text] Then
                c = [ListView].Columns(i)
                Exit For
            End If
        Next

        If IsNothing(c) Then
            Throw New IndexOutOfRangeException(String.Format("Column header text '{0}' does not exist.", [Text]))
        End If

        Return c
    End Function
0
 
wguerramCommented:
Sorry, this is the correct function

  li.SubItems(GetColumnHeaderByName(li, "ColumnText").Text = "no table to drop"


    Public Function GetColumnHeaderByName(ByVal [ListView] As ListView, ByVal [Text] As String) As Integer
        Dim i As Integer

        For i = 0 To [ListView].Columns.Count - 1
            'if the text is found then exit
            If [ListView].Columns(i).Text = [Text] Then
                Exit For
            End If
        Next

        'if the counter is greater than the headers, the text was not found
        If i > [ListView].Columns.Count - 1 Then
            Throw New IndexOutOfRangeException(String.Format("Column header text '{0}' does not exist.", [Text]))
        End If

        Return i
    End Function
0
 
brokeMyLegBikingAuthor Commented:
great, thanks.
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.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now