Link to home
Start Free TrialLog in
Avatar of brokeMyLegBiking
brokeMyLegBiking

asked on

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"
Avatar of RonaldBiemans
RonaldBiemans

Not that I know off :-)

you could add the listview items to a collection (but that seems like a lot of overkill to me)
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
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"

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
ASKER CERTIFIED SOLUTION
Avatar of wguerram
wguerram

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brokeMyLegBiking

ASKER

great, thanks.