What are the requirements for my list/coll开发者_高级运维ection so that it can be sorted in a DataGridView when I click on the column header for sorting?
The best place for this sort of information on the DataGridView is MSDN. For more obscure problems the next port of call is the excellent DataGridView FAQ written by Mark Rideout, the Program Manager for the DataGridView
There is a quite thorough article on data binding and the DataGridView (including sorting) on MSDN titled Custom Data Binding.
Essentially, when binding a data source to the DataGridView sorting works automatically so long as the source implements IBindingList and has supporting code for the interface methods working with sorting (e.g. SupportsSorting should return true
).
Some out of the box data sources do support sorting - the DataTable for example, but most do not.
In particular, the BindingList, though it implements IBindingList does not support sorting. To have a list of objects be sortable you will need to create your own sortable list. There are several example of this on the web, using classes derived from BindingList. Search for SortableBindingList to find one (in fact there is an example in the data binding article I referenced above).
you will need to dig down onto MSDN documentation for that. You can use Auto/Custom sorting depending on your need.
Column Sort Modes in the Windows Forms DataGridView Control
How to: Customize Sorting in the Windows Forms DataGridView Control
from msdn forum: When you click on a column header in a databound DataGridView, it calls the ApplySort method on the IBindingList. You can override the ApplySort method on the BindingSource and perform custom actions there.
Thus my list/collection needs an IBindingList impl.
精彩评论