I am using ListView’s large icon view, to view database inserts with image per insert.
When a new insert is made, I clear the whole ListView and add all inserts using the following code. It works perfectly, only when run for the first time.
The second time (after a new insert is added), I get all the items, but with missing images (some of them are missing and some of them are scrambled)
private void updateListView()
{
myListView.Clear();
myListView.Items.Clear();
myConnection.connect();
List<String> myValues = myConnection.getMyValues();
List<String> myImages = myConnection.getMyImages();
ImageList myImageList = new ImageList();
myImageList.ImageSize = new Size(256, 256);
myImageList.ColorDepth = ColorDepth.Depth32Bit;
for (int i = 0; i < myValues.Count; i++)
{
myListView.Items.Add(myValues[i]);
myListView.Items[i].ImageIndex = i;
myImageList.Images.Add(Image.FromFile(myImages[i]));
}
myListView.LargeImageList = myImageList;
myConnection.close();
myListView.Refresh();
}
I’ve checked with debug and the values/filepaths are correct.
I found it:
The problem was that I had the Sort property of the ListView on.
This means that the ListView sorted it’s Items automatically.
So in these lines:
An item is added and after that the last item (i) is attached to the last image (i)
In between these lines the newly added item is auto-sorted alphabetically, so it isn’t
indexed at i anymore, but something else. So the image is assigned to a random item.
Instead I did this:
And it worked