C# 更新数据绑定组合框
我的问题与此几乎相同: 但是,我正在尝试更改显示的字符串;不添加、删除或排序。我已经尝试了参考问题中提供的BindingList解决方案,但没有任何帮助。 我可以看到,在编辑项目时,combobox的DataSource属性已正确更新,但combobox中显示的内容不是DataSource属性中的内容 我的代码如下所示:C# 更新数据绑定组合框,c#,.net,combobox,refresh,C#,.net,Combobox,Refresh,我的问题与此几乎相同: 但是,我正在尝试更改显示的字符串;不添加、删除或排序。我已经尝试了参考问题中提供的BindingList解决方案,但没有任何帮助。 我可以看到,在编辑项目时,combobox的DataSource属性已正确更新,但combobox中显示的内容不是DataSource属性中的内容 我的代码如下所示: mSearchComboData = new List<SearchData>(); mSearchComboData.Add(new SearchData(""
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";
...
编辑::
RefreshItems似乎是一个私有方法。我刚刚收到错误消息:
“'System.Windows.Forms.ListControl.RefreshItems()'由于其保护级别而无法访问”
重置绑定无效。而不是
SearchComboBox.Refresh()代码>
尝试SearchComboBox.RefreshItems()代码>
或者SearchComboBox.ResetBindings()代码>
我认为你真正需要的是后者
您可以访问其成员的文档。如果要更改整个对象,即整个SearchData对象,则bindinglist将知道此更改,因此正确的事件将在内部触发,组合框将更新。但是,由于您只更新一个属性,bindinglist不知道发生了什么变化
您需要做的是让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";
}
private void按钮1\u单击(对象发送者,事件参数e)
{
//使用一些随机数据填充列表和绑定列表
List dudes=新列表();
添加(新的Dude{Name=“Alex”,年龄=27});
添加(新的Dude{Name=“Mike”,年龄=37});
添加(新Dude{Name=“Bob”,年龄=21});
添加(新的Dude{Name=“Joe”,年龄=22});
this.bindingList=新的bindingList(dudes);
this.comboBox1.DataSource=bindingList;
this.comboBox1.DisplayMember=“Name”;
this.comboBox1.ValueMember=“Age”;
}
私有无效按钮3\u单击(对象发送者,事件参数e)
{
//将所选索引更改为一些随机垃圾
this.bindingList[this.comboBox1.SelectedIndex].Name=“任意”;
}
由于我的类现在实现了INotifyPropertyChanged,因此当某些内容发生更改时,绑定列表会得到“通知”,所有这些都将因此起作用。这是一篇旧文章,但这可能很有用
我刚刚研究了同一个问题,发现如果在BindingList
对象上调用ResetItem
,使用更改的Items位置,它会为您内部引发Itemchanged
通知事件,导致列表更新
int idx = SearchComboBox.SelectedIndex;
mBindingList[idx].Display = value;
mBindingList.ResetItem(idx); //raise Item changed event to update the list display
我尝试了13种不同的方法来更新表单上的组合框,最后找到了这个解决方案。写得很好,很有魅力。谢谢刷新项受保护
int idx = SearchComboBox.SelectedIndex;
mBindingList[idx].Display = value;
mBindingList.ResetItem(idx); //raise Item changed event to update the list display