Results 1 to 5 of 5

Thread: [RESOLVED] Adding Image to ListView - Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Location
    Philippines
    Posts
    218

    Resolved [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.

  2. #2
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    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
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    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
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width