2009-02-02 15 views
7

更新我有几乎同样的问题,因为这一个数据绑定组合框

C# Update combobox bound to generic list

不过,我试图改变所显示的串;不添加,删除或排序。我已经尝试了引用问题中提供的BindingList解决方案,但它没有帮助。 我可以看到ComboBox的DataSource属性在编辑项目时正确更新,但ComboBox中显示的内容不是DataSource属性中的内容。

我的代码如下:

mSearchComboData = new List<SearchData>(); 
mSearchComboData.Add(new SearchData("", StringTable.PatientID)); 
mSearchComboData.Add(new SearchData("", StringTable.LastName)); 
mSearchComboData.Add(new SearchData("", StringTable.LastPhysician)); 
mSearchComboData.Add(new SearchData("", StringTable.LastExamDate)); 

mBindingList = new BindingList<SearchData>(mSearchComboData); 

SearchComboBox.Items.Clear(); 
SearchComboBox.DataSource = mBindingList; 
SearchComboBox.ValueMember = "Value"; 
SearchComboBox.DisplayMember = "Display"; 

... 

当我尝试更新我做了以下的内容:

int idx = SearchComboBox.SelectedIndex; 
mBindingList[idx].Display = value; 
SearchComboBox.Refresh(); 

编辑::

RefreshItems似乎是一个私有方法。我刚刚得到的错误消息:

“‘System.Windows.Forms.ListControl.RefreshItems()’不可访问由于其保护级别”

ResetBindings没有效果。

回答

11

如果您要更改整个对象,意味着您的整个SearchData对象,那么绑定列表就会知道这个更改,因此正确的事件会被触发,并且组合框将会更新。但是,由于您只更新一个属性,绑定列表不知道有什么改变。

你需要做的是让你的SearchData类实现INotifyPropertyChanged。下面是一个简单示例中,我写了证明:

public class Dude : INotifyPropertyChanged 
    { 
     private string name; 
     private int age; 

     public int Age 
     { 
      get { return this.Age; } 
      set 
      { 
       this.age = value; 
       if (this.PropertyChanged != null) 
       { 
        this.PropertyChanged(this, new PropertyChangedEventArgs("Age")); 
       } 
      } 
     } 
     public string Name 
     { 
      get 
      { 
       return this.name; 
      } 

      set 
      { 
       this.name = value; 
       if (this.PropertyChanged != null) 
       { 
        this.PropertyChanged(this, new PropertyChangedEventArgs("Name")); 
       } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 


    } 

下面是一些代码来测试:

 private void button1_Click(object sender, EventArgs e) 
     { 
      //Populate the list and binding list with some random data 
      List<Dude> dudes = new List<Dude>(); 
      dudes.Add(new Dude { Name = "Alex", Age = 27 }); 
      dudes.Add(new Dude { Name = "Mike", Age = 37 }); 
      dudes.Add(new Dude { Name = "Bob", Age = 21 }); 
      dudes.Add(new Dude { Name = "Joe", Age = 22 }); 

      this.bindingList = new BindingList<Dude>(dudes); 
      this.comboBox1.DataSource = bindingList; 
      this.comboBox1.DisplayMember = "Name"; 
      this.comboBox1.ValueMember = "Age"; 

     } 


    private void button3_Click(object sender, EventArgs e) 
    { 
     //change selected index to some random garbage 
     this.bindingList[this.comboBox1.SelectedIndex].Name = "Whatever"; 
    } 

因为我的课现在实现INotifyPropertyChanged,绑定列表被“通知”,当有新的变化,和所有这将会起作用。

+0

我曾试图约13种不同的方法来获得表单更新的组合框,然后终于找到了这个解决方案。写得很好,像魅力一样工作。谢谢! – 2013-06-17 17:42:13

2

代替SearchComboBox.Refresh();

尝试SearchComboBox.RefreshItems();

SearchComboBox.ResetBindings();

我觉得实在是后者,你所需要的。

您可以访问它的会员文档here

+0

RefreshItems被保护 – Maxence 2015-10-07 14:59:51

2

这是一个旧的帖子,但这可能是有用的。

我刚才一直在看同样的问题,已经发现,如果你调用ResetItemBindingList对象上,与更改的项目位置,它内部引发你造成列表更新Itemchanged通知事件。

int idx = SearchComboBox.SelectedIndex; 
mBindingList[idx].Display = value; 

mBindingList.ResetItem(idx); //raise Item changed event to update the list display