[RESOLVED] Adding Image to ListView - Problem
Hi guys,
I'm working on a dynamic listview from Sql server 2k database, and I need to add image on it. I already added ImageList control to my form, and set the ListView property as LargeIcon, SmallImageList as the ImageList.
I do not know how to add image on the listview. Here is my code and I dont know what's wrong with it, though it shows the data from database, only the image is not appearing:
Code:
Public Sub PopulateListView_Images(ByVal lv As ListView, ByVal cmd As SqlCommand, ByVal myImageList As ImageList)
Dim da As New SqlDataAdapter
Dim ds As New DataSet
ds.Reset()
da = New SqlDataAdapter(cmd)
da.Fill(ds)
'Fill Method
lv.Columns.Clear()
lv.Items.Clear()
lv.SmallImageList = myImageList
'Fill Listview
Dim c As DataColumn
For Each c In ds.Tables(0).Columns
'adding names of columns as Listview columns
Dim h As New ColumnHeader
h.Text = c.ColumnName
h.Width = 60
lv.Columns.Add(h)
Next
Dim dt As DataTable = ds.Tables(0)
Dim str(ds.Tables(0).Columns.Count) As String
'adding Datarows as listview Grids
Dim rr As DataRow
For Each rr In dt.Rows
For col As Integer = 0 To ds.Tables(0).Columns.Count - 1
str(col) = rr(col).ToString()
Next
Dim ii As New ListViewItem(str)
lv.Items.Add(ii)
'showing the number of records still added
Next
End Sub
In this code, only the data is showing but no image. I tried adding
Code:
lv.Items(0).ImageKey = 0
but it does not work too.
This is now my table structure:
Code:
SubModule Char 100
Icon Int 4
Only the SubModule name appears. What I need is that the value of "Icon" (0,1,2,3) is dynamic to lv.Items(0).imagekey
Please help me on how to add image on the listview. You can advice me also if my code above is wrong.
Thank you.
Re: Adding Image to ListView - Problem
Ive never really tampered with listview that much except to add rows and such for an extraction process but these links might be of use to you:
1. http://www.sellsbrothers.com/askthew...inlistview.htm
2. http://www.developerfusion.co.uk/show/125/
3. http://www.java2s.com/Tutorial/VB/02...leListview.htm
Re: Adding Image to ListView - Problem
First up, an ImageList is not a control. It's a component.
A ListView gets all its Images from the associated ImageLists. To add a new Image to the ListView you must first add it to the appropriate ImageList, then set the ImageIndex or ImageKey of one or more items to display that Image.
Re: Adding Image to ListView - Problem
Well actually you will need to add your images to the Imagelist first and then set it to the Listview and when adding Listview items you can specify the image index to use for each item.
Re: Adding Image to ListView - Problem
Hi,
Here's is some explanation about a listview and an Imagelist.
http://msdn.microsoft.com/en-us/libr...imagelist.aspx
Wkr,
sparrow1