views:

522

answers:

1

Hi,

I have the following scenario:

  • A database
  • A DataSet that is filled by the database
  • Two Forms:
    • The first Form contains a ComboBox that shows a certain set of categories. The ComboBox is binded to a DataTable "categories" in the DataSet.
    • The second Form is supposed to be a category manager where you can add, edit and delete categories from the database. It contains an editable DataGridView that is also binded to the same DataTable.

Now, when I change the DataSet in the second Form, both the DataGridView and the database are being updated, however the ComboBox in the first Form is not. What's the best way to keep it up to date? Somebody on the MSDN forums suggested something like that:

public void updateDataBindings()
{
    // doesn't seem to do anything:
    categoriesComboBox.DataSource = categoriesBindingSource;
}

This method of Form1 would be called whenever the DataGridView in Form2 changes. The ComboBox however remains unchanged.

Greetings!
wib

+1  A: 

You've databound the dataset to the combobox, but have you also told the combobox what it's datamembers are, etc. so that it knows what value to watch?

David Burela
The DataSource and members are already set elsewhere. I put the line categoriesComboBox.DataSource = categoriesBindingSource;in there in order to "refresh" the ComboBox with the new data. I tried your suggestion and assigned the members to the ComboBox in the updateDataBindigs() as well, to no avail. It still shows categories that have been deleted and so on.
wib
FYI: you don't need to manualy "refresh" your controls. The idea behind databinding is that you connect it up once via databinding, then the data (BindingList, dataset, etc.) raises events automatically when the data changes, then the controls update themselves without you doing anything.This is when bindingSources come in handy. If you go Control -> BindingSource -> Underlying data, then it usually handles it all for you. Try deleting out the rest, and just set up the initial bindings like that
David Burela
David, Yes, that's what I thought! Apparently it doesn't "just work" when two Forms are involved. I found out that each Form seems to have its own instance of the DataSet. I guess that's the problem.
wib