c# - WinForms DataBinding 是否需要 BindingSource 和 BindingList?

标签 c# winforms datagridview bindingsource bindinglist

我想显示 DataGridView 中的人员列表在 Windows 窗体应用程序中。我希望我的服务层返回一个 Person 对象列表(例如 IList<Person> )。我希望列表中的更改反射(reflect)在 DataGridView 中反之亦然。我的理解是使用 BindingSource有助于使用 DataGridView .我的问题是双向数据绑定(bind)工作,我需要:

//pseudo code
BindingSource.DataSource = IBindingList<Person>

或者我可以做:

BindingSource.DataSource = IList<Person>

有什么区别?如果我对列表进行更改,将 DataGridView以任何方式更新?如果我必须使用 BindingList , 返回 BindingList 似乎有点不稳定(因为创建了依赖项)从我的服务层,有办法解决这个问题吗?

Microsoft 表示 BindingList (在备注部分) http://msdn.microsoft.com/en-us/library/ms132679.aspx :

"However, the typical solutions programmer will use a class that provides data binding functionality, such as BindingSource, instead of directly using BindingList<T>."

最佳答案

绑定(bind)到 IList<Person>只会给你单向绑定(bind);对列表或列表项的更改不会反射(reflect)在 DataGridView 中.您可以使用 BindingListBindingSource获得此功能,但您的 Person类仍然需要支持 INotifyPropertyChanged否则您只会在列表中添加项目/从列表中删除项目时获得同步,而不是在列表项目本身更改时。

如果你想避免对 System.Windows.Forms 的依赖, 你可以使用 ObservableCollection<Person>反而;这支持必要的更改通知,因此可以用作双向绑定(bind)源。

关于c# - WinForms DataBinding 是否需要 BindingSource 和 BindingList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4675874/

相关文章:

c# - DataTable的DataColumn中保存图片的DataType

c# - 参数化查询需要未提供的参数 c#

c# - 从 gridview 插入数据时处理 'NULL' 和键约束

C#:调用通过另一个抽象类实现的抽象类方法

c# - 每个 'await' 运算符都会产生一个状态机吗?

c# - 动态表名

c# - 消息中的点击事件

c# - 寻找绘制科学数据的框架 : 2d/3d

c# - 如何遍历表单上的所有复选框?

C# WinForms : Identify type of drag-drop action event