What are the requirements for my list/collection so that it can be sorted in a DataGridView when I click on the column header for sorting?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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).