Listview Filter

  • Follow


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[i].SubItems[1].Text.StartsWith(value) == false)
                {
                    listView1.Items[i].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.
0
Reply Magus 3/19/2010 3:34:44 PM

Magus wrote:
> [...]
> 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
0
Reply Peter 3/20/2010 2:28:12 AM


"Magus" <mr.magus@gmail.com> wrote in message 
news:68c4154b-2b88-4569-89ff-5b6f15d88f85@30g2000yqi.googlegroups.com...
>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. 

0
Reply Andy 3/21/2010 4:28:58 PM

2 Replies
466 Views

(page loaded in 0.054 seconds)

Similiar Articles:
















7/11/2012 10:04:21 AM


Reply: