ListView Filtering

J

jp2msft

I've got a ListBox on my form with lots of data in it (14000 entries).

I have to put a search field on the form. As text is entered into the search
field, I want the ListBox to remove entries that do not match. Sounds simple
enough, but it is really taking a long time! I started running it before
opening my web browser, and now (with typing this message, I'm about to go
over and hit a breakpoint) it is at ... 12687 entries, and all I've entered
is one character into my filtering TextBox.

The data comes from a data table, but we add extra "readability" fields, so
it is not something that is data bound.

Why is my version taking so long?

What can I do to speed it up?

Example Code:
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();
}
}
ListView1.Refresh();
}

Aside: I tried using Visual Studio's Auto Complete setting, but it didn't
work. Looking up the help, it told me that the maximum number of Auto
Complete values depends on the OS (no other information). I presume I was
over running the buffer, so it crapped out on me.
 
T

Tim Jarvis

jp2msft said:
I've got a ListBox on my form with lots of data in it (14000 entries).

Wow, I would say first off don't do that, if you can you should try and
re-architect things so that you don't store that many items in GUI
controls, its simply not good design.

If you really and truely just have to have that much data stuffed into
a listview (and trust me you shouldn't) then put the ListView in
Virtual mode and just pass it the data it wants to display from a
cached list, rather than filter items out of the list (as you found out
the hard way, that is way to slow), just pass it those items that it
needs to satisfy your filter and how many items its displaying.

More info here.

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.vi
rtualmode.aspx#Mtps_DropDownFilterText

Cheers Tim.

--
 
J

jp2msft

Wow, Mr. Jarvis! That is a good article! I'm using Framework 2.0, but the
tutorial page for the Framework 3.5 is *much* better!

I'm going to start working on it right now!
 

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