如何刷新listview中的图像列表图像? [英] How do I refresh images of imagelist in listview?

查看:71
本文介绍了如何刷新listview中的图像列表图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在listview控件中显示图像和一些数据,我遇到了在每一行显示相同图像的问题。

图像路径是网站url。

任何人都可以修改我的源代码

提前谢谢



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

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;
       }

   }





我尝试过:



我使用了imagelist.dispose(),listview.refresh(),listview.dispose()等...



What I have tried:

I used imagelist.dispose() ,listview.refresh(),listview.dispose() etc...

推荐答案

你做了以下错误:



你为每个循环创建列表的新实例。您将一个图像添加到列表并将其分配给列表。你为每个项目做。因此,最终结果是您的图像列表包含一个图像,并且您的列表将该列表作为图像源。



您需要做的是在循环外创建图像列表。将图像添加到循环中的列表中,并在循环完成后将图像列表分配到列表中。



询问是否有不清楚的地方。祝你好运。
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.


这篇关于如何刷新listview中的图像列表图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆