简体   繁体   English

c#当imageList绑定到ListView中时,imageList第一个图像不显示

[英]c# When imageList bind into ListView, imageList 1st image not displaying

I,am having a Listview controller bind with imageList both Listview items and Imagelist images add through a while loop. 我,有一个Listview控制器与imageList绑定Listview项目和Imagelist图像都通过while循环添加。 with dataReader(). 使用dataReader(). the problem is after adding both items, Listview item shows correct but 1st image not displaying means images starting from 2nd listview item. 问题是在添加两个项目之后,Listview项目显示正确,但第一个图像未显示表示从2nd listview项目开始的图像。 I checked every thing with debugging mode. 我用调试模式检查了所有内容。 then I confused on imagelist , because 1st time when it runs it shows COUNT as 1 then next loop again it's COUNT became 0 and the next time it 1,2,3,4,5,6 .... etc. 然后我对imagelist感到困惑,因为第一次运行时它将COUNT显示为1然后下一次循环再次将COUNT变为0 ,而下次将1,2,3,4,5,6 ....等。

private void populate(MySqlDataReader data)
        {
            //ImageList
            ImageList imgList = new ImageList();

            //set listview category items as Large icons
            listView_Category.View = View.LargeIcon;
            imgList.Images.Clear();

            //ADD image list into Listview
            listView_Category.LargeImageList = imgList;

            Application.DoEvents();

            int i = 0;
            while (data.Read())
            {
                try
                {
                    imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/" + data.GetString(2)));
                }
                catch (Exception w)
                {
                    MessageBox.Show(w.Message);
                }

                imgList.ImageSize = new Size(100, 100);
                imgList.ColorDepth = ColorDepth.Depth32Bit;

                listView_Category.Items.Add(data.GetString(1), i);
                i++;

            }
        }

I try to add images manually also... when it is not working. 当它不起作用时,我也会尝试手动添加图像。 only it's adding images through a loop. 只是通过循环添加图像。 is it a Language bug.? 是语言错误吗?

//imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/image-1.jpg"));
            //imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/image-2.jpg"));
            //imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/image-3.jpg"));
            //imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/image-4.jpg"));
            //imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/image-5.jpg"));
            //imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/image-6.jpg"));
            //imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/image-7.jpg"));
            //imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/image-8.jpg"));
            //imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/image-9.jpg"));
            //imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/image-10.jpg"));
            //imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/image-11.jpg"));
            //imgList.Images.Add(Image.FromFile(@"./" + populateFolder + "/image-12.jpg"));

在此处输入图片说明

在此处输入图片说明

Database 数据库

在此处输入图片说明

Please try this this this code will rad all files from a folder and add show it in the listview . 请尝试使用此代码,此代码将导入文件夹中的所有文件,并将其显示在列表视图中。 The you just need to change the logic for reading it form DB . 您只需要更改从DB读取逻辑就可以了。

    private void Form1_Load(object sender, EventArgs e)
    {
        var folderPath = @"c:\images\";
        DirectoryInfo dir = new DirectoryInfo(folderPath);

        var imageList = new ImageList();
        foreach (FileInfo file in dir.GetFiles())
        {
            try
            {
               imageList.Images.Add(Image.FromFile(file.FullName));
            }
            catch{
                Console.WriteLine("This is not an image file");
            }
        }
        this.listView1.View = View.LargeIcon;
        imageList.ImageSize = new Size(128, 128);
        this.listView1.LargeImageList = imageList;

        for (int j = 0; j < imageList.Images.Count; j++)
        {
            ListViewItem item = new ListViewItem();
            item.ImageIndex = j;
            this.listView1.Items.Add(item);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM