Reputation: 193
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.
Upvotes: 4
Views: 20282
Reputation: 1
Child Form inner;
private void UpdateAccount_FormClosed(object sender, FormClosedEventArgs e)
{
ParentForm parentForm= (ParentForm )Application.OpenForms["ParentFormName"];
parentForm.GetAccounts();
}
Parent Form inner;
public void GetAccounts()
{
AccountData lastSelectedItem = (AccountData)cbAccounts.SelectedItem;
cbAccounts.Items.Clear();
List<AccountData> accountDatas = AccountXML.ReadAccountXML();
if (accountDatas != null)
foreach (var item in accountDatas)
{
cbAccounts.Items.Add(item);
}
if(lastSelectedItem != null)
{
cbAccounts.SelectedText = lastSelectedItem.AccountName;
}
}
Upvotes: 0
Reputation: 449
cmbguest.DataSource = null;
loadguestDetails();
make the dataset null and rebind the combobox then u can refresh the data driven combobox in winforms
Upvotes: 0
Reputation: 131
Here is what worked for me. When I refreshed the secondary dataset (first line), the combo box didn't originally pick up the new values, so I reassigned the dataTable as the DataSource (third line).
boundDataSet = proxy.ReadResources();
DataGridViewComboBoxColumn nameColumn = dataGrid.Columns["Name"] as DataGridViewComboBoxColumn;
nameColumn.DataSource = boundDataSet.Table;
Upvotes: 1
Reputation: 228
you have to fill your table adapter from dataset again by
youTableAdapter.Fill(yourdataSet.tablename);
then you have to reassign datasource
this.combobox.DataSource = this.yourBindingSource;
in the end you may refresh your combobox
combobox.Refresh();
Upvotes: 3
Reputation: 3372
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.
Upvotes: 1
Reputation: 4867
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.
Upvotes: 1
Reputation: 7254
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.
Upvotes: 7