how to fill listview with image from database?

i have access table with image fields
i want retrieve all images in the table into listview1  using vbnet
Wesam AhmedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Suvendu Shekhar GiriAssociate LeadCommented:
So, can you please describe the problem doing so? are you getting any issue?
0
Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
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.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wesam AhmedAuthor 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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

John TsioumprisSoftware & Systems EngineerCommented:
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

0
Wesam AhmedAuthor 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
0
John TsioumprisSoftware & Systems EngineerCommented:
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
0
Wesam AhmedAuthor 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

0
Wesam AhmedAuthor Commented:
where are Experts ?????
0
Ryan ChongCommented:
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.
1
Wesam AhmedAuthor 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
0
Ryan ChongCommented:
Coool, glad you found that final answer!
0
John TsioumprisSoftware & Systems EngineerCommented:
I think this what i posted before...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.