I see the three options for "Header Click Sort Action" to be default, multi column, and single column. Is there a way to disable sorting when single clicking? I want to change it to sort on double click.
Is there a way of adding an absolute value sort? The only options I see are ascending, descending, none, and disabled.
Thanks!
-Nick
Question -
Answer - The best way to sort columns other that asc or desc is to use a custom sort comparer. Create a new class which implemenets icomparable and on the initializelayout event of the grid / combo set column.sortcomparer property.
http://msdn.microsoft.com/en-us/library/system.icomparable.aspx
What I want is to do asc, desc, and abs desc, going through with each (double) click. One option is to change the sortcomparer in the AfterSortChange property, but I was hoping for something more elegant than managing the sort state myself.
If this is what I need to do, how do I define a custom sort comparer for built in types? The documentation for SortComparer says it takes to UltraGridCell values. The CompareTo method only takes 1 value.
What I would do is turn off sorting in the grid (HeaderClickAction) and then just handle the DoubleClickHeader event to toggle the SortIndicator property of the column.
Regarding the sort comparer, the interface you need is IComparer, not IComparable.
I'll try that. The SortIndicator is limited to the standard 4 options. I'm going to just set it to Ascending when someone sorts it and maintain which sort I want it to do in the IComparer. Is there a way to change which icon it draws in the header?
I'm also having trouble with sorting... Here's my code
public int Compare(object o1, object o2) { UltraGridCell left = o1 as UltraGridCell; UltraGridCell right = o2 as UltraGridCell; double x = (double)(left.Value); double y = (double)(right.Value); //Other code follows
}
on double x = (double)(left.Value) I get an InvalidCastException. The value will be an int or a double. Is there another way to extract the value?
Depending on the column settings that cell could be a null value. If it is a null value you obviously won't be able to cast it as double.
I would declare a variable as a double and do a double.tryparse on that cell value if that variable you declare is nothing then you don't have a double value.
after you have both your value as double you can just do a return double.compare on both values.
In the debugger, it's not null. It has an integer value.
TryParse worked.
Try doing the double.tryparse to extract the value. If that doesn't work, I would post the issue on a msdn .net forum.