首页 > Windows开发 > 详细

Winfrom中ListBox绑定List数据源更新问题

时间:2015-06-20 18:22:51      阅读:3091      评论:0      收藏:0      [点我收藏+]

Winfrom中ListBox绑定List数据源更新问题

摘自:http://xiaocai.info/2010/09/winform-listbox-datasource-update/

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亦可解决此问题!

Winfrom中ListBox绑定List数据源更新问题

原文:http://www.cnblogs.com/nov5026/p/4590830.html

(1)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!