Click here to Skip to main content
15,042,397 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do I select all items in listview without looping?
I have about 30,000 items, and its taking long time to select.

Can anyone help me with API to use?

Updated 24-May-11 22:20pm
Simon_Whale 25-May-11 4:11am
how are they added to the listview?
That's Aragon 25-May-11 4:21am
Updated for grammar and better readability.
R. Giskard Reventlov 25-May-11 4:22am
Why have you got 30000 items in the list? Does anyone really need to see all of them at the same time???
Hemant__Sharma 25-May-11 4:40am
What type of application you are making a web applicaiton or windows application?
Cool Smith 25-May-11 4:50am
am making a desktop application
Cool Smith 25-May-11 4:51am
the thing is that am loading all from database into List(Of structure) then i update the Listview from this List,what if the user want to scroll or view other items, so what do you suggest?
Sergey Alexandrovich Kryukov 25-May-11 10:22am
Tag it! ASP.NET, WPF, Forms, what?!

Never mind the select: how long does it take to fill? :wtf:

30,000 items in any user interface is too many: how long is it going to take you poor user to actually find the one(s) they want?

There only options are basically: select none, select all. They won't have the time or inclination to do anything else.

Change your interface! Page it, search it, filter it. But don't present the user with more that a hundred or so items at a time!
Cool Smith 25-May-11 4:46am
is there any example this effect/suggestion
johnywhy 14-Dec-16 11:15am
False. You can place a textbox just above the listbox, allowing user to type a filter-string into the textbox. Eg:


Then, your VBA code would filter the contents of the listbox to items that begin with whatever filter-string the user typed. I've done this, and it can be very fast.
What actually is taking so much time? Is it your loop itself (I do believe 30000 iterations last not that long) or are there additional actions slowing the process? You could try to disable event handlers (ItemSelectionChanged and the like, all that may be triggered by selection) or call SuspendLayout()/ResumeLayout() methods before and after selection. Also, if you really have to deal with 30000 items, consider using listview in virtual mode

Disabling event handlers is easy: for example, you do have ItemSelectionChanged handler, listView1_SelectedIndexChanged on your listview, listView1. Then selection logic should look like that:


 // selection loop
  listView1.SelectedIndexChanged+=new EventHandler(listView1_SelectedIndexChanged);

Try/catch block is not necessary, it's added simply to ensure event handler will be restored no matter what, even after exception in loop. If there are more handlers, ItemSelectionChanged, for example, just disable and restore them in the same manner.

Working with layout events suspending is simple as well. Improving previous block of code:


 // selection loop
  listView1.SelectedIndexChanged+=new EventHandler(listView1_SelectedIndexChanged);

Finally, placing listview in virtual mode doesn't even require code :). You can do it from Property Editor, setting VirtualMode property to true (by default it's false). Working with listview this way is harder, though: you have to implement RetrieveVirtualItem event handler and provide VirtualListSize property; also you cannot use Items/SelectedItems collections. There are other limitations as well (see here), so you should decide yourself whether virtual mode suits your project
Cool Smith 25-May-11 5:07am
thanks, pleas point me to example
Kim Togo 25-May-11 6:06am
My 5. Good explanation.
Check out A Much Easier to Use ListView[^].

It has a ListView called FastObjectListView that can build a list of 10,000 objects in less than 0.1 seconds.
Your user never needs so many items at a time. The view should be virtualized. Disagree? Can you explain the use case when all the items are really needed?

OP wrote:
I have about 30,000 items, and its taking long time to select.

It's horrible man.....use paging in result. Load on demand is the best way.

For your reference

SQL Server 2005 Paging Results[^]
Mostly this happen because of event which gets fired after each item is selected. looping through 10,000 will not have impact. So following logic can be implemented
1) Before entering loop remove event handler

this.definitionList.ItemCheck -= new System.Windows.Forms.ItemCheckEventHandler(this.definitionList_ItemCheck);

2) process loop

3) restore event hander again

this.definitionList.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.definitionList_ItemCheck);

4) Add code if you need to do some extra thing on "Select event" which were happening in event handler..

Good Luck

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900