简体   繁体   English

在Windows Form VB.NET中刷新组合框

[英]Refresh combo box in Windows Form VB.NET

Following is the code to populate combobox using DataSource 以下是使用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. 重复此代码不是一个选择,因为它会触发SelectIndexChange事件,并且必须做一些糟糕的工作来避免这种情况。

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. 我实际上以为应该自动与DataSource属性绑定。 I also tried 我也试过

cbCountyList.Refresh()

Thanks in advance. 提前致谢。

You should make a BindingSource sit inbetween the ComboBox and your DataSource. 您应该使BindingSource位于ComboBox和您的DataSource之间。 Then call BindingSource.ResetBindings(false) . 然后调用BindingSource.ResetBindings(false) It will do all the magic for you. 它将为您做所有的魔术。

Here is what I mean (in C#, but same idea): 这是我的意思(在C#中,但想法相同):

BindingSource b = new BindingSource();
b.DataSource = CountryList;
cbCountryList.DataSource = b;
...
...
b.ResetBindings(false); // cbCountryList now has the latest countries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM