Click here to Skip to main content
15,908,581 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
I am displaying images and some datas in listview control and I have faced a problem showing same images on every single row.
The image paths are website url.
Can anybody fix my source code
Thank you in advance

C#
private void Form1_Load(object sender, EventArgs e)
       {
           show_all_items();
       }
       
       public void show_all_items()
       {
           
           lv_main.Items.Clear();

           using (SQLiteConnection conn = new SQLiteConnection(strConn))
           {
               conn.Open();

               string str_select = "SELECT * FROM cars limit 30";

               using (SQLiteCommand cmd = new SQLiteCommand(str_select, conn))
               {
                   using (SQLiteDataReader rdr = cmd.ExecuteReader())
                   {
                       while (rdr.Read())
                       {
                           ListViewItem item = new ListViewItem();

                           ImageList imgList = new ImageList();

                           imgList.Images.Add(LoadImage(rdr["thumnail_url"].ToString()));
                           imgList.ImageSize = new Size(55, 40);
                           
                           lv_main.SmallImageList = imgList;

                           item.ImageIndex = 0;


                           item.SubItems.Add(rdr["carName"].ToString());
                           item.SubItems.Add(rdr["mission"].ToString());
                           item.SubItems.Add(rdr["born"].ToString());
                           item.SubItems.Add(rdr["gas"].ToString());
                           item.SubItems.Add(rdr["mile"].ToString());
                           item.SubItems.Add(rdr["price"].ToString());
                           item.SubItems.Add(rdr["color"].ToString());



                           DateTime regDate = (DateTime)rdr["regDate"];
                           item.SubItems.Add(regDate.ToShortDateString());

                           lv_main.Items.Add(item);


                        }
                       rdr.Close();
                   }
               }

               conn.Close();
           }



       }
       private Image LoadImage(string url)
       {
           System.Net.WebRequest request =
               System.Net.WebRequest.Create(url);

           System.Net.WebResponse response = request.GetResponse();
           System.IO.Stream responseStream =
               response.GetResponseStream();

           Bitmap bmp = new Bitmap(responseStream);

           responseStream.Dispose();

           return bmp;
       }

   }


What I have tried:

I used imagelist.dispose() ,listview.refresh(),listview.dispose() etc...
Posted
Updated 9-May-16 22:50pm
v2

1 solution

You're doing the following wrong:

You create new instance of the list for each loop. You add one image to the list and assign it to the list. You do it for each item. So, final result is that your imagelist contains one image and your list has that list as image source.

What you need to do is create image list outside of the loop. Add images to the list in the loop and once the loop finishes assign the image list to your list.

Ask if something is not clear. Good luck.
 
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