Winfrom中ListBox绑定List数据源更新问题
Winfrom中ListBox绑定List数据源,,第一次可以成功,但后面List更新以后,ListBox并没有更新。
如果 ListBox的数据源 是 DataTable 是可以自动更新的,但若是 List<T> 时对数据的修改界面不会更新,使用 BindingSource 绑定就可以了。
private void InitSample()
{
ListBox listControl = new ListBox();
List<Employee> listSource = new List<Employee>();
BindingSource bs = new BindingSource();
bs.DataSource = listSource;
listControl.DataSource = bs;
listControl.DisplayMember = “Name”;
listControl.ValueMember = “Id”;
// 事先绑定了,这时修改数据源会自动刷新界面显示
listSource.Add(new Employee(1, “Sam”));
listSource.Add(new Employee(2, “John”));
this.Controls.Add(listControl);
}
补充:使用BindingList亦可解决此问题!
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70660.html