ListView control

Posted on 2005-04-21
Last Modified: 2010-04-23
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

Here is where I access the listview:

        li.SubItems(4).Text = "no table to drop"
Question by:brokeMyLegBiking
    LVL 25

    Expert Comment

    Not that I know off :-)

    you could add the listview items to a collection (but that seems like a lot of overkill to me)
    LVL 10

    Expert Comment

    normally to reference them by name i would use a hashtable
    for example:
    dim mynames as new hashtable()

    'then to reference you would
    mynames.item("myname")="Hello World" ' subitems(0).text changes to Hello World
    LVL 25

    Expert Comment

    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)
                mynames.Add("item" & x, li)
                mynames.Add("SubItem" & x, li2)
            DirectCast(mynames("item2"), ListViewItem).Text = "testing"
            DirectCast(mynames("SubItem1"), ListViewItem.ListViewSubItem).Text = "testing sub"

    LVL 8

    Expert Comment

    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

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

            Return c
        End Function
    LVL 8

    Accepted Solution

    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

            '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
    LVL 4

    Author Comment

    great, thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now