In my C# .NET application, I have a DataGridView. The grid's DataSource is a BindingSource, and the BindSource is bound to a filtered DataView of a DataTable that is requeried from SQL frequently.
I can already sort columns in the grid, but the sort is always done alphabetically on the string version of whatever data value is showing in the cells. I have some fields that display a name, but need to be ordered by a specific value instead of alphabetically. I also have a field that can either be a number or the word "Unknown". I need to be able to customize the logic behind the sort.
I know I can't use the grid's SortCompare event because I am using a bound DataSource. I have tried creating columns that display a custom class that derive from IComparable, but sorting on that column still always sorts alphabetically instead of using my Compare() method.
I found one possible solution that involves keeping a hidden DataGridViewColumn that stores a number for sorting on, and using the grid's ColumnHeaderMouseClick event to force it to sort on that hidden "sort" column when the user clicks on the "display" column. However, the column header's "sort glyph" (the little up/down arrow icon that shows up on the sorted column) won't shot up because the actual sorted column is hidden. I have tried manually setting the column's HeaderCell.SortedGlyphDirection property, but the icon will always be hidden if the BindingSource's sort is not for that column.
I'm pretty sure I could get this working if, instead of a DataView of a DataTable, I used a sortable List of a custom wrapper objects I create for each data row, but I don't want to resort to that if possible. But if that's my only option, that's what I'll do.