我使用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++;
}
}
答案 0 :(得分:-1)
请提供数据库行的形状。也许你在DB中有一个错配,所以你称之为不正确的索引。