Some of my objects can contain null values. I use a SortedBindingList to bind these collections to a DataGridView. When clicking on the column headers of the grid the sorting will work in most cases, although if I click to sort a column that contains null values then I get an exception.
Do I need to create a new IComparer, or is there a simpler solution? Maybe the answer is to modify all my objects so that string properties return string.empty instead of null?
Fair enough. Thanks for your answer.
Just out of curiosity, is it better to convert nulls to empty strings in the Fetch method, or to create the object with nulls and only do the conversion to a string in the property getter? Or does it not matter?
I create the objects from calling an Active Directory wrapper which can pass back null strings, nullable bools, etc. The original plan was to keep the objects in sync with the information from AD although I don't think this is necessary.
Thanks for the help
I think the wrapper that I use is rife with nullable bools just because the guy who developed it was so impressed by the new functionality that he had to use them all through his code. In most cases I just cast them to a bool
I ran into a similar situation and modified the CompareTo method in the SortedBindingList code to look like this
public int CompareTo(ListItem other) { object target = other.Key; // Updated to support nulls if (Key == null && target == null) return 0; else if (Key == null) return -1; else if (target == null) return 1; if (Key is IComparable) return ((IComparable)Key).CompareTo(target); else { if (Key.Equals(target)) return 0; else return Key.ToString().CompareTo(target.ToString()); } }
Copyright (c) Marimer LLC