R
Ray Mitchell
Hello,
In my application I need to periodically remove all current items from a
ListView and add a new set into it. The following abbreviated code contains
the basic idea:
private void FillListViewFromFile(string path)
{
// listView = new ListView();
// listView.Clear();
for (int ix = listView.Items.Count - 1; ix >= 0; --ix)
listView.Items.RemoveAt(ix);
string line;
while ((line = sr.ReadLine()) != null)
{
// ...use Split to put fields into result array, etc...
string[] result;
// Add "result" array item to ListView
listView.Items.Add(new ListViewItem(result));
}
}
The problem is with properly removing all the items. I finally resorted to
the "for" loop shown to remove them one at a time from the bottom up, and it
all works perfectly. But it doesn't seem that this is the most elegant way
to do it. However, when I try either of the two techniques that I've
commented out, the ListView always displays as empty after adding the new
items. Can you please explain why and what I need to do to get them to work
correctly?
Thanks,
Ray
In my application I need to periodically remove all current items from a
ListView and add a new set into it. The following abbreviated code contains
the basic idea:
private void FillListViewFromFile(string path)
{
// listView = new ListView();
// listView.Clear();
for (int ix = listView.Items.Count - 1; ix >= 0; --ix)
listView.Items.RemoveAt(ix);
string line;
while ((line = sr.ReadLine()) != null)
{
// ...use Split to put fields into result array, etc...
string[] result;
// Add "result" array item to ListView
listView.Items.Add(new ListViewItem(result));
}
}
The problem is with properly removing all the items. I finally resorted to
the "for" loop shown to remove them one at a time from the bottom up, and it
all works perfectly. But it doesn't seem that this is the most elegant way
to do it. However, when I try either of the two techniques that I've
commented out, the ListView always displays as empty after adding the new
items. Can you please explain why and what I need to do to get them to work
correctly?
Thanks,
Ray