简体   繁体   中英

How do I add an image to a new Listview Item?

Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader
dis = dr("Last Accessed").Substring(0, dr("Last Accessed").IndexOf(" "))
ListView1.Items.Add(New ListViewItem(New String() {docImage, dr("Id").ToString, dr("Document Name").ToString, dr("DISnummer"), dr("XPostnummer"), dis}))

I use this code to fill a listview but I like to add a little image to the left of the rows. I connected an imagelist but the code for adding the image doesnt work:

If dr("[Document Type]").ToString = "PDF" Or dr("[Document Type]").ToString = "pdf" Then
   docImage = (ImageList1.Images.Count - 1)
Endif

What am I doing wrong?

M

After some testing I figured it out.

While dr.Read
            If dr("Document Type").ToString = "PDF" Or dr("Document Type").ToString = "pdf" Then
                docImage = (ImageList1.Images.Count - 1)
            ElseIf dr("Document Type").ToString = "DOC" Or dr("Document Type").ToString = "doc" Then
                docImage = (ImageList1.Images.Count - 3)
            ElseIf dr("Document Type").ToString = "PPT" Or dr("Document Type").ToString = "ppt" Then
                docImage = (ImageList1.Images.Count - 2)
            End If

            dis = dr("Last Accessed").Substring(0, dr("Last Accessed").IndexOf(" "))
            items.Add(New ListViewItem(New String() {"", dr("Document Name").ToString, dr("DISnummer"), dr("XPostnummer"), dis, dr("Id").ToString, dr("Document Location").ToString}, docImage))
        End While

The image is at the end of the items.Add function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM