how to fill listview with image from database?

Wesam Ahmed
Wesam Ahmed used Ask the Experts™
on
i have access table with image fields
i want retrieve all images in the table into listview1  using vbnet
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Suvendu Shekhar GiriAssociate Lead

Commented:
So, can you please describe the problem doing so? are you getting any issue?
Usually you create an imagelist at design time, and then load the images into that.

The listview has an imagelist property which you can set directly to the imagelist, if the imagelist is on the same form. Alternatively you can pass it on imagelist object, e.g. if you prefer to have the imagelist be on its own form, and reference it in many places. But for starters, just place it on the same form as your listview. The imagelist control itself is not visible at runtime.

In the imagelist, each image shall be given a name which becomes the image ID. When loading the listview, you pass it the name of the image you wish to use.

Author

Commented:
this my code work only without image ,  its show the text without image

 
 '  local variables
        Dim Conn As New OleDbConnection(ConStr)
        Dim command As OleDbCommand
        Dim data_reader As OleDbDataReader
        'setup listview
        setupListView()
        'connect to ms.access database

        Conn.Open()
        'reading data from TblInstitution table
        command = New OleDbCommand("SELECT * FROM cars_tbl", Conn)
        data_reader = command.ExecuteReader

        'add data to listview
        If data_reader.HasRows Then
            While data_reader.Read
                Dim newitem As New ListViewItem()

                Dim imageListLarge As New ImageList()
                newitem.Text = data_reader.GetValue(1) 'first column
                ' newitem.SubItems.Add(data_reader.GetValue(1)) 'second column
                imageListLarge.Images.Add(Bitmap.FromStream(data_reader.GetValue(3)))
                ListView1.Items.Add(newitem)

            End While
        End If

Open in new window

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

John TsioumprisSoftware & Systems Engineer

Commented:
I think you should add this :
ListView1.LargeImageList = imageListLarge 

Open in new window

and then modify
ListView1.Items.Add(newitem,indexofImageFromImageList)

Open in new window

EDIT: i just tested the codeclipListView.jpg
Me.ListView1.LargeImageList = Me.imageListLarge 
        Me.ListView1.Items.Add("xx", 0)
        Me.ListView1.Items.Add("x2", 1)

Open in new window

Author

Commented:
John Tsioumpris
i want load the image from my database table inside list view like the image you send
i have filed name and img for the picture.
i can load the name and img filed in datagridview normaly , but in listview i cant only i can load the name filed
John TsioumprisSoftware & Systems Engineer

Commented:
EDIT2 : This works only when the Listiview's view  is set to LargeList/Smallist/Tile
If you want to use it with details then it needs more work as you need to draw it...the process is described here

Author

Commented:
OK this another code work but not display image for each item in listview
the image stored in database

xx.JPG
 Dim Conn As New OleDbConnection(ConStr)
        Dim ds As New DataSet
        Conn.Open()
        Dim sqladap As New OleDbDataAdapter("SELECT * FROM cars_tbl", Conn)
        sqladap.Fill(ds)

        For i As Integer = 0 To ds.Tables(0).Columns.Count - 1
            ListView1.Columns.Add(ds.Tables(0).Columns(i).ToString())
        Next
        

        For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
            Dim listRow As New ListViewItem
            listRow.Text = ds.Tables(0).Rows(i)(1).ToString()

            For j As Integer = 1 To ds.Tables(0).Columns.Count - 1
                listRow.SubItems.Add(ds.Tables(0).Rows(i)(j).ToString())
            Next
            ListView1.Items.Add(listRow)
        Next

Open in new window

Author

Commented:
where are Experts ?????
Ryan ChongSoftware Team Lead

Commented:
Anders already explained that to you at Comment ID: 42378177.

Usually you create an imagelist at design time, and then load the images into that.

so depends on how you actually stored that "image" in your Access database (As attachment or image path, etc?), you got to load these images into an imagelist control and use it for your listview.

Author

Commented:
Hello geeks :)

after search and take Notes from Anders, i change the scenario.

before i save the image inside the database and this not possible for listview, so you need to change the table design

i will get the image path and save it in database and add the key of this image in img_key field

then i create code to load the image path and add it to listimage1 , then add the key with item name in listview

cars.JPG
The code here
Dim Conn As New OleDbConnection(ConStr)
        Dim ds As New DataSet
        Conn.Open()
        Dim sqladap As New OleDbDataAdapter("SELECT * FROM cars_tbl", Conn)

        sqladap.Fill(ds)
        For x As Integer = 0 To ds.Tables(0).Rows.Count - 1
            Dim m As Integer = ds.Tables(0).Rows(x)(5) ' for the image key
            ImageList1.Images.Add(Image.FromFile(Application.StartupPath & "\cars\" & ds.Tables(0).Rows(x)(4).ToString()))
            ListView1.Items.Add(ds.Tables(0).Rows(x)(1).ToString(), m)
        Next

Open in new window


Thanks
Ryan ChongSoftware Team Lead

Commented:
Coool, glad you found that final answer!
John TsioumprisSoftware & Systems Engineer

Commented:
I think this what i posted before...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial