Filtering ListViewItems

J

Johnny J.

I would like to implement a filter function on a ListView control, and I
found this code on TheCodeProject:

http://www.codeproject.com/cs/miscctrl/listviewfilter.asp

Since it's not possible to toggle the ListViewItem's visibility, the
filtering is acheived (in this case) by moving the "non-matching" item to a
temporary collection and returning them to the ListViewItem collection when
they are no longer filtered out.

That approach works ok in this case, because the listviewitems are sorted,
but if they are not, the CodeProject code will not work, because items that
are reinserted in the ListViewItem collection are merely added and will thus
appear at the bottom of the list.

Is there any way of easily keeping track of the original order and
reinserting the items in their original position?

Cheers,
Johnny J.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Johnny J. said:
I would like to implement a filter function on a ListView control, and I
found this code on TheCodeProject:

http://www.codeproject.com/cs/miscctrl/listviewfilter.asp
Is there any way of easily keeping track of the original order and
reinserting the items in their original position?

I can think of a couple of ways to achieve it.
Maybe you can remember the index at where the item was when y ou removed it
the first time, then when you move it back in you know where to put it.

another possibility is to simply rebind the list everything you change it.
you hold the "original" list outside and make a view of it that it the one
that you bind to the control, when you need to change it you simply do it
again.
 
I

Ido Samuelson

If you use datasets you can use the DataView object to achieve filtering
(and binding of course).
Or you can create a class using the decorator pattern and add filtering to
your data objects. then you simply bind your decorator to the listview. And
when you change the filtering options on the decorator, the listview will be
update accordingly.
 
N

Nicholas Paldino [.NET/C# MVP]

Unfortunately, the ListView does not support data binding for the items
in the views, so this wouldn't do much.
 
I

Ido Samuelson

true, however there are implementation for binding to listview.

http://www.codeproject.com/cs/miscctrl/ListView_DataBinding.asp is a nice
example.

Nicholas Paldino said:
Unfortunately, the ListView does not support data binding for the items
in the views, so this wouldn't do much.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ido Samuelson said:
If you use datasets you can use the DataView object to achieve filtering
(and binding of course).
Or you can create a class using the decorator pattern and add filtering
to your data objects. then you simply bind your decorator to the
listview. And when you change the filtering options on the decorator, the
listview will be update accordingly.
 
J

Johnny Jörgensen

Sorry I was unclear:

Databinding has nothing to do with my question. As Nicholas points out, the
ListView is not databindable, and even if it was, it would still not solve
my problem. I'm looking for a way to do this without databinding.

As for Ignacios suggestion about remembering the items index, it's an idea,
but a very complex one. If it was only ONE item that was temoved, it
wouldn't be a problem, but now were talking about an undefined number
between 0 and all items. So when I want to reinsert an item, I have to
iterate through ALL the items that are in the Listview to find out which
Items i should insert my item between. And the items that are left in the
listview doesn't have their origianl indexes anymore, so i would be forced
to match all the items with a table of the original indexes.

I can write the code for that, but all solutions I can see are going to take
time, thus affecting performance during the filtering process.

I guees I hoped that somebody would have a magical solution... ;-)

Cheers,
Johnny




Ido Samuelson said:
true, however there are implementation for binding to listview.

http://www.codeproject.com/cs/miscctrl/ListView_DataBinding.asp is a nice
example.

Nicholas Paldino said:
Unfortunately, the ListView does not support data binding for the
items in the views, so this wouldn't do much.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ido Samuelson said:
If you use datasets you can use the DataView object to achieve filtering
(and binding of course).
Or you can create a class using the decorator pattern and add filtering
to your data objects. then you simply bind your decorator to the
listview. And when you change the filtering options on the decorator,
the listview will be update accordingly.

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.com> wrote
in message Hi,

I would like to implement a filter function on a ListView control, and
I found this code on TheCodeProject:

http://www.codeproject.com/cs/miscctrl/listviewfilter.asp


Is there any way of easily keeping track of the original order and
reinserting the items in their original position?

I can think of a couple of ways to achieve it.
Maybe you can remember the index at where the item was when y ou
removed it the first time, then when you move it back in you know where
to put it.

another possibility is to simply rebind the list everything you change
it. you hold the "original" list outside and make a view of it that it
the one that you bind to the control, when you need to change it you
simply do it again.
 
N

Nicholas Paldino [.NET/C# MVP]

Johnny,

I don't think that noting the indexes of the items that are filtered out
is that big of a deal. If you cycle through the list items, and remember
the zero-based indexes of the items that are removed (along with the items),
then you can iterate through that list of items that was removed again, and
call Insert on the Items collection with that index. You just have to make
sure that as you iterate through the removed items, the index that you are
inserting at is incrementing.

Every time you call Insert, it will arrange the items between the insert
point and the next insert point so that they are in the correct position.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Johnny Jörgensen said:
Sorry I was unclear:

Databinding has nothing to do with my question. As Nicholas points out,
the ListView is not databindable, and even if it was, it would still not
solve my problem. I'm looking for a way to do this without databinding.

As for Ignacios suggestion about remembering the items index, it's an
idea, but a very complex one. If it was only ONE item that was temoved, it
wouldn't be a problem, but now were talking about an undefined number
between 0 and all items. So when I want to reinsert an item, I have to
iterate through ALL the items that are in the Listview to find out which
Items i should insert my item between. And the items that are left in the
listview doesn't have their origianl indexes anymore, so i would be forced
to match all the items with a table of the original indexes.

I can write the code for that, but all solutions I can see are going to
take time, thus affecting performance during the filtering process.

I guees I hoped that somebody would have a magical solution... ;-)

Cheers,
Johnny




Ido Samuelson said:
true, however there are implementation for binding to listview.

http://www.codeproject.com/cs/miscctrl/ListView_DataBinding.asp is a nice
example.

Nicholas Paldino said:
Unfortunately, the ListView does not support data binding for the
items in the views, so this wouldn't do much.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

If you use datasets you can use the DataView object to achieve
filtering (and binding of course).
Or you can create a class using the decorator pattern and add filtering
to your data objects. then you simply bind your decorator to the
listview. And when you change the filtering options on the decorator,
the listview will be update accordingly.

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.com> wrote
in message Hi,

I would like to implement a filter function on a ListView control, and
I found this code on TheCodeProject:

http://www.codeproject.com/cs/miscctrl/listviewfilter.asp


Is there any way of easily keeping track of the original order and
reinserting the items in their original position?

I can think of a couple of ways to achieve it.
Maybe you can remember the index at where the item was when y ou
removed it the first time, then when you move it back in you know
where to put it.

another possibility is to simply rebind the list everything you change
it. you hold the "original" list outside and make a view of it that it
the one that you bind to the control, when you need to change it you
simply do it again.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

As for Ignacios suggestion about remembering the items index, it's an
idea, but a very complex one. If it was only ONE item that was temoved, it
wouldn't be a problem, but now were talking about an undefined number
between 0 and all items. So when I want to reinsert an item, I have to
iterate through ALL the items that are in the Listview to find out which
Items i should insert my item between. And the items that are left in the
listview doesn't have their origianl indexes anymore, so i would be forced
to match all the items with a table of the original indexes.
I can write the code for that, but all solutions I can see are going to
take time, thus affecting performance during the filtering process.

I guees I hoped that somebody would have a magical solution... ;-)

It's not that complex, and you can implement it and share with the rest of
us :)
 

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