Results 1 to 3 of 3

Thread: [RESOLVED] Strange imageIndex problem

  1. #1

    Thread Starter
    Lively Member bcass's Avatar
    Join Date
    Dec 2007
    Location
    Island of Dots
    Posts
    108

    Resolved [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?

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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

  3. #3

    Thread Starter
    Lively Member bcass's Avatar
    Join Date
    Dec 2007
    Location
    Island of Dots
    Posts
    108

    Re: Strange imageIndex problem

    Crap, yeah, didn't spot the overload. Cheers, that works perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width