0

Following is the code to populate combobox using DataSource

Dim CountryList As Array = MyCtrl.GetAllCountries
    With cbCountyList
      .DataSource = CountryList 
      .DisplayMember = "CountryName"
      .ValueMember = "CountryID"
    End With

After adding a new COuntry Name to Database I want to reflect the changes in combobox. Repeating this code is not an option becasue it triggers SelectIndexChange event and had to do some crappy work around to avoid that.

So I was wondering if there is way to refresh the combobox list. I actually thought binding with the DataSource property supposed to do it automatically. I also tried

cbCountyList.Refresh()

Thanks in advance.

1 Answer 1

5

You should make a BindingSource sit inbetween the ComboBox and your DataSource. Then call BindingSource.ResetBindings(false). It will do all the magic for you.

Here is what I mean (in C#, but same idea):

BindingSource b = new BindingSource();
b.DataSource = CountryList;
cbCountryList.DataSource = b;
...
...
b.ResetBindings(false); // cbCountryList now has the latest countries
1
  • 1
    This wasn't working for me, until I realised I was INSERTing my new record into the database underneath, not the datatable.
    – bendecko
    Apr 3, 2014 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.