Listview Filter

M

Magus

I have a listview with around a 1000 items. I'm using the following
code to filter the list:
private void Filter(string value)
{
for (int i = listView1.Items.Count - 1; -1 < i; i--)
{
if
(listView1.Items.SubItems[1].Text.StartsWith(value) == false)
{
listView1.Items.Remove();
}
}

The problem is that I have no way to restore the old contents of the
listbox. I've tried using:

ListViewItem[] listarray = new ListViewItem[ListView1.Items.Count];

What happens though is that I get an error explaining that the items
can't exist twice. So I looked up some information about using Clone()
but I can't seem to find a method to use it in.

Any help would be appreciated. I'm basically just trying to copy the
contents, and revert when the user has found the info he's looking for.
 
P

Peter Duniho

Magus said:
[...]
The problem is that I have no way to restore the old contents of the
listbox. I've tried using:

ListViewItem[] listarray = new ListViewItem[ListView1.Items.Count];

What happens though is that I get an error explaining that the items
can't exist twice. So I looked up some information about using Clone()
but I can't seem to find a method to use it in.

You can't have the same ListViewItem object in the ListView at once.
But it should work fine to keep your ListViewItem instances in an array
and repopulate the entire ListView from that as needed. You'll just
need to clear the ListView first, or make sure you don't try to re-add
ListViewItems that are already there.

If you're sure you're doing exactly that and it still doesn't work, you
should post a concise-but-complete code example that reliably
demonstrates the problem.

Pete
 
A

Andy O'Neill

Magus said:
I have a listview with around a 1000 items. I'm using the following
code to filter the list:

If this is a windows app or a low usage web app then I'd consider a
datatable to hold your data.
You can use defaultview to filter what the user sees and then just take the
filter back off or apply a different one.
Datatables are kind of heavyweight but they make some things dead easy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top