我想显示 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 usingBindingList<T>
."
最佳答案
绑定(bind)到 IList<Person>
只会给你单向绑定(bind);对列表或列表项的更改不会反射(reflect)在 DataGridView
中.您可以使用 BindingList
或 BindingSource
获得此功能,但您的 Person
类仍然需要支持 INotifyPropertyChanged
否则您只会在列表中添加项目/从列表中删除项目时获得同步,而不是在列表项目本身更改时。
如果你想避免对 System.Windows.Forms
的依赖, 你可以使用 ObservableCollection<Person>
反而;这支持必要的更改通知,因此可以用作双向绑定(bind)源。
关于c# - WinForms DataBinding 是否需要 BindingSource 和 BindingList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4675874/