Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ListView control

Posted on 2005-04-21
6
Medium Priority
?
307 Views
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
        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
Comment
Question by:brokeMyLegBiking
6 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13833617
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
 
LVL 10

Expert Comment

by:gangwisch
ID: 13836463
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13841196
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:wguerram
ID: 13843051
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
 
LVL 8

Accepted Solution

by:
wguerram earned 1800 total points
ID: 13843123
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
 
LVL 4

Author Comment

by:brokeMyLegBiking
ID: 13843502
great, thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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 (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

810 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