Click here to Skip to main content
15,878,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a database application in which i have to load about a thousand list of items from the database into a combobox. The retrieving and the population of the data into the combobox is done successfully using the
foreach
loop. But the problem is that it takes too long a time for the populating of the combobox to get completed because of the large list of items to populate.

Can any folk help me with an idea about how to populate the combobox in the fastest way possible? I will be very thankful for that. Thank you.
Posted
Updated 28-Jun-18 0:11am

Rather than explicitly loading the data into the combo box an item at a time, why don't you bind the data to it once you've retrieved it. This is generally a lot more efficient.

However, is there a reason you need to display that many items. In my experience, users will not want to wade through that many items to find one that they want. Rather than loading 1000 items, why not provide AutoComplete instead?
 
Share this answer
 
Comments
Dylan Morley 8-Mar-12 9:48am    
Totall agree with your second paragraph - 1000 is far too many to be displaying in a drop down list.

For the benefit of the OP, here's the only thing I could quickly find on the subject
http://www.ixda.org/node/19338

Notice people are saying you shouldn't have more than 12 \ 30 etc?

There are better ways to display data than drop down lists when working with larger sets of data. Drop down lists are best suited for a small selection of possibilities.

AutoComplete is certainly a way to go :)
Either use Items.AddRange[^] to add all the items at the same time, or wrap your loop in BeginUpdate[^]/EndUpdate.

Edit: but indeed, this is way too many items to put in a dropdown, you should use an auto-completing textbox or something similar instead.
 
Share this answer
 
v2
Thousand is not a big number. Ideally it should not take long to process them. Are you sure it is the loop that takes so much time?
 
Share this answer
 
Comments
BobJanova 8-Mar-12 9:49am    
If it's causing a UI refresh each time, it certainly can be.
In stead of ForEach, you can try Parallel.ForEach in System.Threading.Tasks namespace.
C#
Parallel.ForEach 


Here is the sample code:
C#
IEnumerable<string> items = ...

Parallel.ForEach(items, item => {
   ...
});
</string>
 
Share this answer
 
Comments
BobJanova 8-Mar-12 9:49am    
This will not help at all for updating a UI control.

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