-
listview columns
I have a listview with three columns. I want to load a table of names into the listview, putting last names A-F in #1, G-S in #2, and T-Z in #3.
I know how to loop through the table and load the names, but I don't know the listview code to separate the names into columns. Any ideas?
-
I use this to load values from an array I have.
Code:
Private Sub Form_Load()
Dim i As Integer
Dim Item As ListItem
For i = 0 To UBound(Jobs)
Set Item = lvwDayDetails.ListItems.Add(, , Jobs(i).Name)
Item.Tag = Jobs(i).ID
Item.ToolTipText = Jobs(i).Subject
Item.SubItems(1) = Format(Jobs(i).NextInvocation, "hh:mm")
Item.SubItems(2) = Jobs(i).Duration
Next i
Set Item = Nothing
End Sub
-
Well ...
The ListItem.Text property will give you the first column. ListItem.SubItems(1) will give you the second column, ListItem.SubItems(2) will give you the third column and so on.
.
-
What I am looknig for is an "if...then" statement of some kind. For example, if the last name begins with "a" then put it into column #1. But I can't quite figure out the listview syntax for this logic.
There has to be some allowance for one column being longer than the other. For example, column #3 might have 25 names, while column #1 might have 2.
-
I think you might have to place three distinct listboxs on your
form. You can size them to make it LOOK like they are one, but in
reality they are different.
Check into arrays of controls, I'v never done those but it might be
what you want.
Essentially, if you need to hardwire 3 columns, set up the three
different list controls and set the recordsource with a sql query:
select NAME from xxx where name>="A" and name<"H";
this will get you A-G,.....
I know this will work, but I'm not sure if there is a more elegant
method of doing it. A multi-column listbox is supposed to be
similar in nature to a table in that the data in column x is
supportive and related to the data in column x+1. In your
scheme the only relation is that they are all names. I'm not being
very clear.....sorry.
HTH
-
The way a listview works is each row is an Item. The first column of each row is the text of the item. Each column after that is a subitem of the first item.
So in my example I put the Name in the first column (Item), subject in the second column (SubItem1), and duration in the last column (SubItem2).
-
Well ...
Off the top of my head, use three variables, each will track how many items you have got in each column.
Traverse through the items you have, if you find it starts from A, put it in the first column, increment first counter by 1, if it starts from G then put it in 2nd column, increment second counter by 1 and so on.
Code:
Count1 = 0
Count2 = 0
Count3 = 0
For Each Item In YourCollection
If Item starts with A - F
If Count1=ListView1.ListItems.Count Then
'Add a listitem. It will be blank in the beginning.
New ListItem.Text = Item
Else
Get ListView's Count1-th item
Set its Text = Item.
End If
Count1 = Count1 + 1
ElseIf Item starts with G - S
If Count2=ListView1.ListItems.Count Then
'Add a listitem. It will be blank in the beginning.
New ListItem.Subitems(1) = Item
Else
Get ListView's Count2-th item
Set its SubItems(1) = Item.
End If
Count2 = Count2 + 1
Else
If Count3=ListView1.ListItems.Count Then
'Add a listitem. It will be blank in the beginning.
New ListItem.SubItems(2) = Item
Else
Get ListView's Count3-th item
Set its SubItems(2) = Item.
End If
Count3 = Count3 + 1
End If
Next Item
That's not proper VB, but that should give you some idea.
.