[RESOLVED] Strange imageIndex problem
I have a ListView (lvwDocs) which has 2 columns (DocLink and ID). Here's the code I'm using to populate the ListView:
Code:
Do While dataReader.Read
itmListItem = New ListViewItem()
itmListItem.Text = dataReader(0)
For shtCounter = 1 To dataReader.FieldCount() - 1
If dataReader.IsDBNull(shtCounter) Then
itmListItem.SubItems.Add("")
Else
itmListItem.SubItems.Add(dataReader(shtCounter))
End If
Next shtCounter
lvwDocs.Items.Add(itmListItem)
This populates all columns and all rows as expected.
However, I want to add an icon next to each row. To achieve that, I change the last line of the code to:
Code:
lvwDocs.Items.Add(itmListItem.Text, nIndex)
The variable nIndex is initialised to 0 before the Do While loop. This successfully adds the desired icon, however, the 2nd ListView column is no longer populated, only the first column. Can anyone help get both columns populated, as per the 1st example, as well as the icon?
Re: Strange imageIndex problem
It's because you are using an overload of the Add method that is creating the ListViewItem for you. Try this:
Code:
Do While dataReader.Read
itmListItem = New ListViewItem()
itmListItem.Text = dataReader(0)
itmListItem.ImageIndex = nIndex
For shtCounter = 1 To dataReader.FieldCount() - 1
If dataReader.IsDBNull(shtCounter) Then
itmListItem.SubItems.Add("")
Else
itmListItem.SubItems.Add(dataReader(shtCounter))
End If
Next shtCounter
lvwDocs.Items.Add(itmListItem)
Loop
Re: Strange imageIndex problem
Crap, yeah, didn't spot the overload. Cheers, that works perfectly.