SortedBindingList DataGridView sorting with null strings

SortedBindingList DataGridView sorting with null strings

Old forum URL: forums.lhotka.net/forums/t/478.aspx


pfeds posted on Tuesday, June 27, 2006

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?

pfeds replied on Tuesday, June 27, 2006

As an afterthought, I imagine this would also occur without the SortedBindingList with normal binding to a collection...

ajj3085 replied on Tuesday, June 27, 2006

Don't use nulls.  In the database, there may be a difference between an empty string an a null, but there isn't really in the UI.  (Unless there's something you didn't mention which null vs. empty string in the business layer matters).

Andy

pfeds replied on Tuesday, June 27, 2006

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.

 

 

ajj3085 replied on Tuesday, June 27, 2006

I usually do this in the Fetch or create methods.  Nullable bools are a bit different though, because you may not be able to choose what the null means.  Is it false, true, or does null have a different meaning in AD?

OTOH, will you be sorting by boolean values?  If not, you don't need to worry.

Andy

pfeds replied on Tuesday, June 27, 2006

Thanks for the help Big Smile [:D]

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 Stick out tongue [:P]

ajj3085 replied on Tuesday, June 27, 2006

Ha... Well, i've been known to do that as well.  If your BO doesn't really allow for null bools, then you certainly shouldn't be using Nullable<bool> anywhere.  Go smack that other developer.  Smile [:)]

Andy

claptik replied on Friday, July 14, 2006

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());
        }
      }

xal replied on Friday, July 14, 2006

I solved this and other problems with sorting in general by altering the SortedBindingList just a little bit. This is a very small change.

Here's a link to an older thread in the old forum:
LINK

It basically consists in providing values for the sorting to do what you expect. This is done very easily through an event.

Andrés

Copyright (c) Marimer LLC