Wednesday, September 06, 2006

ComboBox DataSource And Refresh Issues

How many times have you been frustrated by the fact , that you set the DataSource on a
ComboBox and it does Not show up or it does not Refresh .

basically,
code like
myComboBox.DataSource = value

does not update the values on screen

the workaround to get this working though not elegant is :

BindingManagerBase manager = myComboBox.BindingContext(value);
manager.SuspendBinding();
myComboBox.DataSource = value;
manager.ResumeBinding();

4 comments:

Pec said...

This doesn't seem to work for me. I get an "Error: BindingContext is a 'property' but is used like a 'method'". Are you sure this code example compiled for you? I'm having this same problem and was looking for a workaround, ugly or not. :)

Arthur May said...

I had this problem too, the sort property on the control was set to true, should be false.

AvaniMehta said...

Hey even I was facing this problem. Removing Sort did work. However, is there any way to have both? I need sorting on my combobox

mugnyte said...

// optional : unhook any eventing
cb.SelectedIndexChanged -= cb_SelectedIndexChanged;

// reset binding cb.BindingContext[cb.DataSource].SuspendBinding();

cb.BindingContext[cb.DataSource].ResumeBinding();

// reselect item
cb.SelectedItem = /* edited item */;

// optional : re-hook any eventing
cb.SelectedIndexChanged += cb_SelectedIndexChanged;