Using an ORM approach in applications can often lead to the scenario where you have a collection of objects you’ve retrieved and would like to display them in a tabular view using a DataGridView.
In my (limited) experience, binding collections of objects using a custom BindingList to a DataGridView results in poor performance and unsatisfactory sorting. I’m looking for a generic solution to this problem such that it’s straightforward to populate a DataGridView and also extract the underlying objects later.
I will describe a good solution I’ve found, but I’m looking for alternatives.
I am going to preface this answer with the statement that my experience is within the 2.0 Framework domain. The newer Frameworks may offer other solutions.
Below is a BindingList derived class that supports bi-directional sorting and other useful features of List. I can’t take credit for the PropertyComparer sorting. I found this in an article a while back, but now I see it is all over the Internet so I unfortunately cannot cite the original source.
Another alternative is the BindingListView: http://blw.sourceforge.net. This class allows you to create a view of a List collection, like you would with a DataTable object, including the ability to define a Filter and a Sort.