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(objDropdow nQuery.cat egory)
li.SubItems.Add(objDropdow nQuery.act ive)
lvwMain.Items.Add(li)
Here is where I access the listview:
li.SubItems(4).Text = "no table to drop"
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_
li.SubItems.Add(objDropdow
li.SubItems.Add(objDropdow
lvwMain.Items.Add(li)
Here is where I access the listview:
li.SubItems(4).Text = "no table to drop"
normally to reference them by name i would use a hashtable
for example:
dim mynames as new hashtable()
mynames.add("myname",li.Su bItems(0). Text)
mynames.add("myname2",li.S ubItems(1) .Text)
'then to reference you would
mynames.item("myname")="He llo World" ' subitems(0).text changes to Hello World
for example:
dim mynames as new hashtable()
mynames.add("myname",li.Su
mynames.add("myname2",li.S
'then to reference you would
mynames.item("myname")="He
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("col 1", 50, HorizontalAlignment.Center )
ListView1.Columns.Add("col 2", 50, HorizontalAlignment.Center )
For x As Integer = 0 To 10
Dim li As New ListViewItem
Dim li2 As ListViewItem.ListViewSubIt em
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("SubIte m1"), ListViewItem.ListViewSubIt em).Text = "testing sub"
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("col
ListView1.Columns.Add("col
For x As Integer = 0 To 10
Dim li As New ListViewItem
Dim li2 As ListViewItem.ListViewSubIt
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"
DirectCast(mynames("SubIte
Here is a function to get a column header by its text:
Public Function GetColumnHeaderByName(ByVa l [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(S tring.Form at("Column header text '{0}' does not exist.", [Text]))
End If
Return c
End Function
Public Function GetColumnHeaderByName(ByVa
Dim c As ColumnHeader
For i As Integer = 0 To [ListView].Columns.Count - 1
If [ListView].Columns(i).Text
c = [ListView].Columns(i)
Exit For
End If
Next
If IsNothing(c) Then
Throw New IndexOutOfRangeException(S
End If
Return c
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
great, thanks.
you could add the listview items to a collection (but that seems like a lot of overkill to me)