views:

325

answers:

5

What is the preferred method of refreshing a combo box when the data changes?

If a form is open and the combo box data is already loaded, how do you refresh the contents of the combo box without the form having to be closed and reloaded?

Do you have to do something on the Click event on the combo box? This would seem to be a potential slow down for the app if there is a hit to the database every time someone clicks on a combo box.

+1  A: 

You must determine:

1) When does you data change?

If it depends on other users activity, so you can't determine whether it's changed without querying DB, you can figure out an optimal time for a refresh, like form loading or on every click, or you can use a timer control to refresh the data in a specific time.

2) When does your user need to know about that change?

Try to understand how urgent it is for the user to know about a change. Talk to them. Depending on that, decide when do you need to refresh your data.

Finally:

There isn't a correct way of doing that. It depends on a software structure, users' needs and on a specific situation.

Hope it helps. Good Luck!

UPDATE:

I can add a solutions, that I used recently. If something won't be clear, just ask.

I assume, your refreshing the combo from MS SQL Server.

If so,

1. Create a table , storing in it Combo's data changing date or a version.

2. onClick event or using timer control, which will check for changes every 5 minutes(or any other time), you can compare last change date (or version) of your combo with last change(or version) in that table we store last date(or version) and only if the date(or version) was changed, refresh the combo.

3. Last date (or version) you can store in a variable or in a textbox control, changing it's value every time you refresh the combo.

4. Update last date(or version) in that table if the data changes.

In this case, you'll just need to check for changes, not update them.

P.S. If this solution doesn't feet you, just refresh every time on click event. There's no better event for that case.

hgulyan
A: 

Depends on how many people will be using the form but in normal circumstances, using the onclick event of the select box is fine. Using an ajax call is good because it means you dont have to load the entire page.

Luke
@Luke, It's win form, I guess.
hgulyan
A: 

One thing is clear that you are using Dropdown means not more items you need to load in the dropdowm i think near about 20 or 30. Then, what is the problem in database call ? create Procedure that will use the execution plan and give you fast result. or put a table which you need to load in cache and fill your cache at certain time if data is change then load the data in dropdown. I am working in Window application i am facing same thing but there is no better option then call database or put it in the cache.

KuldipMCA
A: 

I can see two ways of doing this:

  1. Put a "Refresh" button in UI and reload data only when the user clicks the button. It should be clear to the user (descriptive label, message box or whatever) that by hitting refresh its current selection(s) might change.

  2. Monitor data changes in the database for the combo's underlying table. When data changes, the UI may either update immediately or just store a flag about data having changed (more on this later). In order to know rapidly when data changes, a database trigger seems the best solution to me: the trigger (UPDATE, INSERT, DELETE) is set on the combo's underlying table and increments a counter (datetime, version, whatever floats your boat) in a separate table created only for this purpose. Every time the combo is repopulated (including form load), the counter's value is attached (tag?) to it, to be compared with the current database value. Getting the current counter value could be done on a timer.

Now, if the two counters are different there are two options:

A. Update the UI immediately. I would normally hate such an UI but, not knowing what your actual requirements are, this may go as an option.

B. Set a flag that the UI should be updated. On the dropdown event, check the flag: if it's set, start by repopulating the combo.

In most situations I'd go without any refresh at all or with the first solution but it really depends on requirements.

HTH.

EDIT:

The purpose of the trigger/counter setup is not only to get change info fast but to actually know if data changed, which would be much more complicated to accomplish by directly querying the underlying table. Sorry if this wasn't clear in my initial post (or even after this addition) but English is not my native tongue.

Sorin Comanescu
A: 

Question 1: ComboboxName.Clear and then ComboboxName.Items.AddItem for each item. Question 2: Of course this depends on how often the data changes and how big the list is, but I would probably put a timer set for every minute or so. This will prevent too many hits to the DB and will make sure your form isn't taking too much time filling in values to the combobox.

DaMartyr