简体   繁体   English

在C#中绑定到DataGridView时使用的好集合

[英]A good collection to use when binding to a DataGridView in C#

What would be the best collection to use when binding a list of data to a DataGridview in C#? 将数据列表绑定到C#中的DataGridview时,最好的集合是什么? I'm currently using just a Generic List but the data grid doesn't update when there is objects added or removed from the list. 我目前只使用通用列表,但是当列表中添加或删除对象时,数据网格不会更新。 I've looked at using a BindingList or a ObservableCollection, but can't decide which would be best to use that would update and be easy to sort/filter without having to rebind to the data grid. 我已经看过使用BindingList或ObservableCollection,但是无法决定哪个最好使用哪个会更新并且很容易排序/过滤而不必重新绑定到数据网格。 I'm currently working in windows form on .Net 3.5 framework with plans to move over to WPF soon. 我目前正在.Net 3.5框架上以windows形式工作,计划很快转移到WPF。

  • ObservableCollection<T> won't work for a DataGridView : it implements INotifyCollectionChanged , not IBindingList , and the DataGridView doesn't know about INotifyCollectionChanged . ObservableCollection<T>不适用于DataGridView :它实现INotifyCollectionChanged ,而不是IBindingList ,而DataGridView不知道INotifyCollectionChanged It is intended for WPF bindings and is not used in Windows Forms 它适用于WPF绑定,不用于Windows窗体
  • BindingList<T> is a good option, but note that it doesn't support sorting or filtering out of the box. BindingList<T>是一个不错的选择,但请注意它不支持排序或过滤。 However, you can find some custom implementations of these features on the web. 但是,您可以在Web上找到这些功能的一些自定义实现。
  • DataTable is probably your best option if you need sorting and/or filtering capability 如果您需要排序和/或过滤功能, DataTable可能是您的最佳选择

The data binding framework is completely different between WinForms and WPF, so (in general), there isn't a "best choice" for the both of them. 数据绑定框架在WinForms和WPF之间完全不同,因此(通常),它们都没有“最佳选择”。

For WinForms, using the generic BindingList<T> will accomplish most of what you want (though it doesn't handle changes to individual items; you'll have to implement that yourself). 对于WinForms,使用通用BindingList<T>将完成您想要的大部分内容(尽管它不处理对单个项的更改;您必须自己实现)。

For WPF, ObservableCollection<T> serves a similar purpose. 对于WPF, ObservableCollection<T>具有类似的用途。

Actually Microsoft reccomends using a Collection as your binding collection rather than a List because of the ability to do automatic functions like when adding and removing items, clearing the collection, or setting the value of an existing item. 实际上,Microsoft建议使用Collection作为绑定集合而不是List,因为它能够执行自动功能,例如添加和删除项目,清除集合或设置现有项目的值。

Collection Class on MSDN. MSDN上的集合类

If you want to bind a collection to a datagridview, I'd use a bindinglist. 如果要将集合绑定到datagridview,我将使用绑定列表。 Although a bindinglist does not support sorting out of the box you can extend it by creating your own list that derives from BindinList and implement your own sorting. 虽然绑定列表不支持开箱即用,但您可以通过创建自己的列表来扩展它,该列表派生自BindinList并实现您自己的排序。

See here on how to do this. 请参阅此处了解如何执行此操作。

A DataTable, perhaps? 也许是DataTable?

Also, you can often force the gridview to repaint and include the new items by calling DataGridview.Invalidate() immediately after items are added. 此外,您通常可以通过在添加项目后立即调用DataGridview.Invalidate()来强制gridview重新绘制并包含新项目。

I do not think there's a hard/general rule of what type of collection is best suited for DataGridView. 我不认为哪种类型的集合最适合DataGridView有一个硬/一般规则。

It really depends on a several factors: 这实际上取决于几个因素:
- The nature of the data - 数据的性质
- What are the operations (if any) to be performed from the UI to the DB (eg CRUD, sort, filter) - 从UI到DB执行的操作(如果有的话)(例如CRUD,排序,过滤)
- Size of the data - 数据大小
etc etc.. 等等..

I created my own collection inheriting from BindingList which supports sorting, filtering, etc. It works well now but was a lot of work, I don't really recommend doing one yourself... I think there's one already implemented on CodeProject or a similar site, I'll give you the link if I find it. 我创建了自己的集合继承自BindingList,它支持排序,过滤等。它现在运行良好,但是很多工作,我真的不建议你自己做一个...我认为已经在CodeProject上实现了一个或类似的网站,如果我发现它,我会给你链接。

Edit: 编辑:

I found this CodeProject article with an implementation of IBindingList which supports sorting, filtering, etc. I didn't test it though, so I don't know how good the implementation is, but it might be worth checking it out. 我发现这个CodeProject文章带有IBindingList的实现,它支持排序,过滤等等。我没有测试它,所以我不知道实现有多好,但它可能值得一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM