我有一个Listview
控制器绑定imageList
两个Listview
项和Imagelist
图片通过while循环添加。使用dataReader().
问题是在添加两个项目后,Listview项目显示正确但第一个图像不显示意味着从2nd
listview项目开始的图像。我用调试模式检查了每件事。然后我对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++;
}
}
我尝试手动添加图像......当它不工作时。只有它通过循环添加图像。这是一个语言错误。?
//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"));
数据库
答案 0 :(得分:1)
请尝试此操作此代码将显示文件夹中的所有文件并添加在列表视图中显示。您只需要更改从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);
}
}