views:

236

answers:

3

I have a winform with a combo box thats filled from a query in the database. If I add a field to the database, the new field won't show up in the form until I close it and reopen it.

I was able to put in a MessageBox.Show() and once that popped up, I closed it, and saw the new data in the combo box.

EDIT:

Let me clarify a bit. I have a dropdown combo box, and that is populated by a table adapter. I just did the databinding with the GUI so I'm not sure how it works.

What I want is, I want the new data that I enter to be refreshed when I come back to it. I have a seperate window to manage the data, and then I close it I want the combo box to be updated with what I just saved.

Is this possible? I tried to do it on form load but that doesn't work either, I think because the form is already loaded.

+2  A: 

The Refresh method is not for that. What you want to achieve is refreshing the data binding. This would be sth like this:

cb.DataBindings[0].ReadValue();

Another way is using a data source that supports change notification. Such a data source fires ListChanged event with appropriate parameters to trigger update of controls that are bound to it.

kubal5003
I have tried adding a few things to the ListChanged event, and nothing seems to redraw the control. Maybe a better question would be, how do you redraw a combobox that's popluated by a bound dataset?
Christof
I have a dataset with a list of items, that are just logins from the database. Its bound through a binding source to a table adapter. When I pop up another form, it is used to modify the data, you can add logins and other information, then click save, and close the form. But when you go back to that parent form, the combo box does not show the new information unless you close the program and reopen it again, if that makes sense.
Christof
+1  A: 

It depends on what your datagrid is bound to. Repopulating the datasource (like a DataTable or a customized BindingList) should automatically repopulate the grid, assuming the data source's ListChanged event is properly fired.

Sorin Comanescu
The only time the ListChanged event is fired is when you open up the program. I set the ListChanged to a method that just popped up a message box, and it came up about 5 times while opening the program, then it never changed after that.
Christof
I think you've got that a little bit wrong. ListChanged is an event fired by DATASOURCE. AFAIK BindingSource does not support change notification thus it doesn't fire ListChanged and that is why DataGridView or ComboBox, or whatever is bound to it, doesn't refresh its contents. To fully use binding capabilities of winforms I had to write my own version of a collection that implements IBindingList with everything that is there(change notification, sorting, searching)
kubal5003
@kubal5003: BindingSource has a ListChanged event which occurs when the underlying list changes.
Sorin Comanescu
+1  A: 

If I understand this correctly, one thing you could do is use a ListChanged event like what was mentioned but it seems as if that didn't work.

TableAdapters aren't really a table in the standard sense but a temporary storage area.

Look in your Form1_Load function (or whatever you named your form, just using the default) and look for a tableadapter.fill method. this.comboboxTableAdapter.Fill(yourdataset name). This is what actually fills your dataset.

Create a function that fills those datasets (if you have more than one) and then call that function on a ListChanged event, or even on the Activate event of the form. This way when you go into that child form and change the data, when you come back to main form the data will be there.

I hope this helps, and good luck with your project.

Jeremy Morgan