Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Filtering ListViewItems

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Johnny J. - 26 Sep 2007 15:33 GMT
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.
Ignacio Machin ( .NET/ C# MVP ) - 26 Sep 2007 15:46 GMT
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.
Ido Samuelson - 26 Sep 2007 16:41 GMT
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.

> Hi,
>
[quoted text clipped - 14 lines]
> that you bind to the control, when you need to change it you simply do it
> again.
Nicholas Paldino [.NET/C# MVP] - 26 Sep 2007 16:58 GMT
Unfortunately, the ListView does not support data binding for the items
in the views, so this wouldn't do much.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> If you use datasets you can use the DataView object to achieve filtering
> (and binding of course).
[quoted text clipped - 22 lines]
>> the one that you bind to the control, when you need to change it you
>> simply do it again.
Ido Samuelson - 26 Sep 2007 17:32 GMT
true, however there are implementation for binding to listview.

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

>    Unfortunately, the ListView does not support data binding for the items
> in the views, so this wouldn't do much.
[quoted text clipped - 25 lines]
>>> the one that you bind to the control, when you need to change it you
>>> simply do it again.
Johnny Jörgensen - 26 Sep 2007 20:22 GMT
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

> true, however there are implementation for binding to listview.
>
[quoted text clipped - 30 lines]
>>>> the one that you bind to the control, when you need to change it you
>>>> simply do it again.
Nicholas Paldino [.NET/C# MVP] - 26 Sep 2007 21:44 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Sorry I was unclear:
>
[quoted text clipped - 53 lines]
>>>>> the one that you bind to the control, when you need to change it you
>>>>> simply do it again.
Ignacio Machin ( .NET/ C# MVP ) - 27 Sep 2007 21:11 GMT
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
[quoted text clipped - 4 lines]
> 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 :)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.