Custom sorting a column in a databound DataGridView control.
-
I am trying to implement a simply task that appears to be amazingly hard to accomplish in .Net: Sorting a column in a datasource bound control (e.g. DataGridView), not based on the underlying values (i.e. simple string comparison on the values) but on some custom rule that I want to define. I have a DataGridView that is bound to a Bindingsource which in turn is (via Dataview, .Net internal stuff...) bound to a Datatable. This is plain vanilla stuff, out of the box. If I was happy with the default sort behavior, i.e. sorting columns by the values in DataTable, everything would be easy, even automatic. However, some of the contents in the Datatable is not in a user-friendly format, so it must be formatted for display in the datagridview (e.g. by overriding the CellFormatting event). If I now sort the control by that column, the rows will be sorted by the underlying values, and not by the displayed text, which is bad. I cannot use the Sort() function nor the IComparer overwrite of the DataGridView since I am working in databound mode. How in the world then can I intercept the sorting mechanism and tell the DataGridView how to sort this column properly? It strikes me as odd that the standard data source - binding source - control model in .Net appears to have no provision at all for custom sorting! What is even odder is that a primary reason for the whole concept of a BindingSource is just sorting and filtering! There must be an easier way than writing 1000s of lines of code to create my own DataView or IBindingList derived class that will allow this kind of customization, as recommended in various articles. This seems such a common scenario that I can't believe there is a more elegant way...
-
I am trying to implement a simply task that appears to be amazingly hard to accomplish in .Net: Sorting a column in a datasource bound control (e.g. DataGridView), not based on the underlying values (i.e. simple string comparison on the values) but on some custom rule that I want to define. I have a DataGridView that is bound to a Bindingsource which in turn is (via Dataview, .Net internal stuff...) bound to a Datatable. This is plain vanilla stuff, out of the box. If I was happy with the default sort behavior, i.e. sorting columns by the values in DataTable, everything would be easy, even automatic. However, some of the contents in the Datatable is not in a user-friendly format, so it must be formatted for display in the datagridview (e.g. by overriding the CellFormatting event). If I now sort the control by that column, the rows will be sorted by the underlying values, and not by the displayed text, which is bad. I cannot use the Sort() function nor the IComparer overwrite of the DataGridView since I am working in databound mode. How in the world then can I intercept the sorting mechanism and tell the DataGridView how to sort this column properly? It strikes me as odd that the standard data source - binding source - control model in .Net appears to have no provision at all for custom sorting! What is even odder is that a primary reason for the whole concept of a BindingSource is just sorting and filtering! There must be an easier way than writing 1000s of lines of code to create my own DataView or IBindingList derived class that will allow this kind of customization, as recommended in various articles. This seems such a common scenario that I can't believe there is a more elegant way...
AFAIK you must apply your (custom) sort on the underlying data structure, not the DGV itself. So set the DGV to programmatic sorting, handle the cell click event (checking for row<0 and getting the column index), and sort your DataTable or whatever is underneath the DGV. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3