Andrew Davies created an excellent little class on sourceforge called BindingListView<T> which essentially allows you to bind a collection to a DataGridView while supporting sorting and filtering. Binding a DataGridView to a normal List<T> does not support sorting and filtering, as the proper interfaces are not implemented by List<T>.
The class works great and has solved my UI problems. However, it’d be awesome if I could iterate through the collection using LINQ, but I’m just not sure how to set it up to do so. Source code can be downloaded here. Can anyone help me out?
Because the
BindingListView<T>project uses .NET Framework v2.0 and predates LINQ, it doesn’t expose anIEnumerable<T>for you to query on. Since it does implement non-genericIEnumerableand non-genericIList, you can useEnumerable.Cast<TResult>to convert the collection into a form suitable for use with LINQ. However, this approach isn’t that helpful because theIEnumerablethatAggregateBindingListView<T>returns is an internal data structure with typeKeyValuePair<ListItemPair<T>, int>.To upgrade this project for convenient use with LINQ, the simplest approach might be to implement
IEnumerable<T>onAggregateBindingListView<T>. First add it to the declaration of the class:and then implement it at the end of the class definition:
and now you can use LINQ directly on a
BindingListView<T>instance like this:Remember to upgrade all the projects from .NET Framework v2.0 to .NET Framework 4 Client Profile and add
using System.Linq;in order for this to work with your current project.