列表视图项和图像列表项不匹配

时间:2017-01-06 11:08:09

标签: c# winforms listview

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

但是当程序运行1st列表视图项未设置为正确的图像1st listview item second 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++;
            }


        }

1 个答案:

答案 0 :(得分:-1)

请提供数据库行的形状。也许你在DB中有一个错配,所以你称之为不正确的索引。