列表<T>与 BindingList<T>优点缺点 [英] List&lt;T&gt; vs BindingList&lt;T&gt; Advantages/DisAdvantages

查看:26
本文介绍了列表<T>与 BindingList<T>优点缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能为我的项目描述一下两者之间的区别.

Can someone describe what the difference between the two are for my project.

目前我有一个 List 并将 BindingSource 设置为那个,将 DataGridView 设置为 BindingSource.

Currently I have a List<MyClass> and set the BindingSource to that and a DataGridView to the BindingSource.

我已经实现了 IEditableObject,所以当 CancelEdit 被调用时,我将我的对象恢复到使用 Memberwise.Clone()

I have implemented IEditableObject so when CancelEdit is called I revert my object back to what it was with a Memberwise.Clone()

将我的 List 更改为 BindingList 是否可以解决这些问题?使用 BindingList 有什么好处?

Will changing my List to a BindingList solve any of this and what are the advantages of using a BindingList?

推荐答案

List<> 只是一个自动调整大小的数组,由给定类型的项目组成,带有几个辅助函数(例如:排序).它只是数据,您可能会使用它来对模型中的一组对象运行操作.

A List<> is simply an automatically resizing array, of items of a given type, with a couple of helper functions (eg: sort). It's just the data, and you're likely to use it to run operations on a set of objects in your model.

BindingList<> 是一个类型化列表或集合的包装器,它实现了 IBindingList 接口.这是支持双向数据绑定的标准接口之一.它通过实现 ListChanged 事件来工作,该事件在您添加、删除或设置项目时引发.绑定控件侦听此事件以了解何时刷新其显示.

A BindingList<> is a wrapper around a typed list or a collection, which implements the IBindingList interface. This is one of the standard interfaces that support two-way databinding. It works by implementing the ListChanged event, which is raised when you add, remove, or set items. Bound controls listen to this event in order to know when to refresh their display.

当您将 Bi​​ndingSource 的数据源设置为 List<> 时,它会在内部创建一个 BindingList<> 来包装您的列表.如果您想在 BindingSource 之外访问它,您可能希望自己用 BindingList<> 预先包装您的列表,但除此之外它是一样的.您还可以从 BindingList<> 继承以在更改项目时实现特殊行为.

When you set a BindingSource's DataSource to a List<>, it internally creates a BindingList<> to wrap your list. You may want to pre-wrap your list with a BindingList<> yourself if you want to access it outside of the BindingSource, but otherwise it's just the same. You can also inherit from BindingList<> to implement special behavior when changing items.

IEditableObject 由 BindingSource 处理.当您更改任何绑定控件中的数据时,它会在任何实现对象上调用 BeginEdit.然后,您可以在 BindingSource 上调用 EndEdit/CancelEdit,它会将其传递给您的对象.移动到不同的行也会调用 EndEdit.

IEditableObject is handled by the BindingSource. It'll call BeginEdit on any implementing object when you change the data in any bound control. You can then call EndEdit/CancelEdit on the BindingSource and it will pass it along to your object. Moving to a different row will call EndEdit as well.

这篇关于列表<T>与 BindingList<T>优点缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆