【问题标题】:Listview items and Image list items missmatchingListview 项目和图像列表项目不匹配
【发布时间】:2017-01-06 11:08:09
【问题描述】:

我使用ListViewImageList 创建了一个图像网格视图。image names 来自database,图像存储在categories 文件夹中。我添加图片imgList.Images.Add("", Image.FromFile(@"./categories/" + dr["image"]));

但是当程序运行1st 列表视图项未设置为正确的图像时,1stlistviewitemsecond image,我认为存在索引错误。我的代码有问题还是有什么方法可以解决这个问题?

private void LoadCategories() {
            DataTable categories;
            con = new Dbfunctions();
            con.MysqlQuery("SELECT * FROM categories WHERE online = 1");
            categories = con.QueryEx();

            //ImageList

            ImageList imgList = new ImageList();

            //CLEAR listview_Category items
            listView_Category.Items.Clear();

            //set listview category items as Large icons
            listView_Category.View = View.LargeIcon;

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


            int i = 0;
            //ADD image into imagelist and Listview
            foreach(DataRow dr in categories.Rows){
                imgList.Images.Add("", Image.FromFile(@"./categories/" + dr["image"]));
                ListViewItem category = new ListViewItem();

                //bind listview item vwith image list item
                category.ImageIndex = i;

                //set Category name
                category.Text = dr["name"].ToString();

                //set font list items styles
                category.Font = new System.Drawing.Font("Courier New", 15, System.Drawing.FontStyle.Regular);
                category.ForeColor = System.Drawing.Color.FromArgb(252, 119, 123);

                //ADD category items into Listview
                listView_Category.Items.Add(category);

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

                i++;
            }


        }

【问题讨论】:

  • 看起来不错。你确定不匹配吗??
  • 是的,第一张图片没有加载...第二张图片是第一张

标签: c# winforms listview


【解决方案1】:

请提供 DB 行的形状。也许您在 DB 中存在不匹配,因此您调用了不正确的索引。

【讨论】:

  • 这不是答案!移到评论和删除!!
  • 我还不能发表评论(声誉低下),我愿意帮助发行人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 2014-01-05
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 2018-08-13
相关资源
最近更新 更多