Link to home
Create AccountLog in
Avatar of Wesam Ahmed
Wesam Ahmed

asked on

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
Avatar of Suvendu Shekhar Giri
Suvendu Shekhar Giri
Flag of India image

So, can you please describe the problem doing so? are you getting any issue?
ASKER CERTIFIED SOLUTION
Avatar of Anders Ebro (Microsoft MVP)
Anders Ebro (Microsoft MVP)
Flag of Denmark image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Wesam Ahmed
Wesam Ahmed

ASKER

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

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 codeUser generated image
Me.ListView1.LargeImageList = Me.imageListLarge 
        Me.ListView1.Items.Add("xx", 0)
        Me.ListView1.Items.Add("x2", 1)

Open in new window

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
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
OK this another code work but not display image for each item in listview
the image stored in database

User generated image
 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

where are Experts ?????
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.
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

User generated image
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
Coool, glad you found that final answer!
I think this what i posted before...