Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

Please suggest me in adding an image to an item in the listview?

regards,
Chaithu
Posted
Updated 27-Nov-17 19:54pm
v2
Comments
Sergey Alexandrovich Kryukov 20-May-11 21:10pm    
Tag it! WPF, Forms, ASP.NET, what?!
--SA

If you want to add the images to the ListView by code, you can do something like this:

private void Form10_Load(object sender, EventArgs e)
        {
            DirectoryInfo dir = new DirectoryInfo(@"c:\pic");
            foreach (FileInfo file in dir.GetFiles())
            {
                try
                {
                    this.imageList1.Images.Add(Image.FromFile(file.FullName));
                }
                catch{
                    Console.WriteLine("This is not an image file");
                }
            }
            this.listView1.View = View.LargeIcon;
            this.imageList1.ImageSize = new Size(32, 32);
            this.listView1.LargeImageList = this.imageList1;
            //or 
            //this.listView1.View = View.SmallIcon;
            //this.listView1.SmallImageList = this.imageList1;
 
            for (int j = 0; j < this.imageList1.Images.Count; j++)
            {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = j;
                this.listView1.Items.Add(item);
            }
        }


Hope this might help you.
 
Share this answer
 
Comments
M.CHAITHANYA 20-May-11 6:24am    
Hi,

In the foreach loop when i use the File class which is static ,it is throwing an error.Can't we use the static type in the foreach loop?
Hi,

Please have a look at the following links:

ListView with Image on SubItems[^]
ListView.Items Property[^]

Kind regards,
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900