windows 如何刷新 Winform 上的数据驱动组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1705194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:25:00  来源:igfitidea点击:

How to refresh data driven combo box on a winform

c#.netwindowsvisual-studiowinforms

提问by Christof

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.

我有一个带有组合框的 winform,该组合框是从数据库中的查询填充的。如果我向数据库中添加一个字段,新字段将不会显示在表单中,直到我关闭它并重新打开它。

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.

我能够放入 MessageBox.Show() ,一旦它弹出,我将其关闭,然后在组合框中看到新数据。

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.

让我澄清一下。我有一个下拉组合框,它由一个表适配器填充。我只是用 GUI 进行了数据绑定,所以我不确定它是如何工作的。

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.

这可能吗?我尝试在表单加载时执行此操作,但这也不起作用,我认为是因为表单已加载。

回答by kubal5003

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

Refresh 方法不是为了那个。您想要实现的是刷新数据绑定。这将是这样的:

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.

另一种方法是使用支持更改通知的数据源。此类数据源使用适当的参数触发 ListChanged 事件,以触发与其绑定的控件的更新。

回答by Ehtsham

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();

回答by AdIch

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).

这对我有用。当我刷新辅助数据集(第一行)时,组合框最初没有选择新值,因此我将 dataTable 重新分配为 DataSource(第三行)。

boundDataSet = proxy.ReadResources();
DataGridViewComboBoxColumn nameColumn = dataGrid.Columns["Name"] as DataGridViewComboBoxColumn;
nameColumn.DataSource = boundDataSet.Table;

回答by Sorin Comanescu

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.

这取决于您的数据网格绑定到什么。假设数据源的 ListChanged 事件已正确触发,重新填充数据源(如 DataTable 或自定义 BindingList)应自动重新填充网格。

回答by Jeremy Morgan

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.

如果我理解正确,你可以做的一件事就是使用一个 ListChanged 事件,就像上面提到的那样,但似乎这不起作用。

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

TableAdapter 并不是标准意义上的真正表,而是一个临时存储区域。

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.

查看您的 Form1_Load 函数(或任何您为表单命名的函数,只需使用默认值)并查找 tableadapter.fill 方法。this.comboboxTableAdapter.Fill(您的数据集名称)。这实际上是填充您的数据集的内容。

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.

创建一个函数来填充这些数据集(如果您有多个),然后在 ListChanged 事件上调用该函数,甚至在表单的 Activate 事件上调用该函数。这样,当您进入该子表单并更改数据时,当您返回主表单时,数据将在那里。

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

我希望这会有所帮助,并祝您的项目好运。

回答by Taja_100

         cmbguest.DataSource = null;
         loadguestDetails();

make the dataset null and rebind the combobox then u can refresh the data driven combobox in winforms

使数据集为空并重新绑定组合框,然后您可以在 winforms 中刷新数据驱动的组合框

回答by Nurullah Yayan

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;
        }

    }