views:

436

answers:

2

When sorting on a column in the GridView bound to an Enum, it sorts by the order of the Enum. I need it to sort by the string representation of the Enum. Here are the options I have seen, of which I like none of them.

  1. Reorder the Enum in alphabetical order - Bad because now the presentation is relying on the Business and Data Access Layer to "pre-sort" the data.
  2. Create a new object (datatable, new list, whatever) with myEnum.ToString() and bind this to the GridView - This one is not bad, but I would rather not.
  3. In my search, check to see if the column sorted is an Enum, then sort by the string representation of the column - Do I have to say why this is bad?

Number 2 would be my favorite so far, but like I said, I don't like it.

More info just in case - I am binding a List of IWhatever to the grid, and 2 columns are enums that need to be sorted by strings. There are also guid-type, string, and decimal columns in the grid that need to be sorted.

+1  A: 

Try to use ViewModels. Basically you create ViewModel Objects that contains your Model Object (your IWhatever). This ViewModel exposes then new Properties and Methods which are used in your View. In Your case you would expose a property with the string representation of your Enum. The advantage is, that you could do any transformation logic you want.

See MVVM Pattern. http://en.wikipedia.org/wiki/Model%5FView%5FViewModel

EDIT: Little Example:

public class WhateverViewModel
{
    public WhateverViewModel(IWhatever model)
    {
        this.model = model;
    }
    ...
    public string MyEnumView
    {
        get
        {
            return model.MyEnum.ToString(); 
        }
    }
    public string MyEnumView2
    {
        get
        {
            switch(model.MyEnum)
            {
                case MyEnumType.A: return "Hello";
                case MyEnumType.B: return "World";
            } 
        }
    }
}
Arthur
A: 

It sounds like you want a SortExpression for a column which will make the default sort routine understand your intention to order the objects in your data source by string representation of an enumeration which is not possible. The SortExpression only identifies the property to sort on, not how the data type itself is sorted.

If the values of the enumeration don't matter, you can specify values for the order you want without having to reorder them on your, but its essentially the same thing. If the values do matter, you can obviously throw this out of the window. I do not like the idea or reorganizing the values explicitly in code and not specifying values either because it would end up being a nightmare to maintain. I thought it might be possible to apply attributes to the enumeration values that would be reflected in the sort, but it turns out to not be possible.

Since your goal is to change how an enumeration is sorted, you are left with either option 2 or option 3. I don't see the problem with option 3 so much as long as the sort algorithm is contained within the data source itself. In fact it is a rather succinct implementation with LINQ. However, if you decide to use method 2 you can still use the default sort implementation, and that may be more attractive to some people.

NickLarsen
The values do not matter, but like I said in my question, it is a matter of seperation of concerns. The Enum resides in the Business Layer, and the Business Layer doesn't care how it is sorted, only the Presentation Layer cares.
Martin
As for your suggestion of using #3, it removes all aspects of a dynamic sort, and I am left with basically a manual sort (if this column sort this way, if this column sort ...), and that is definitely not want I want. I actually did go with #2 ... but it was justify because I need it for others reasons as well.
Martin
I agree with your point on separation of concerns, I was just investigating as many options as I could think of to give you the magic SortExpression. I don't think #3 removes dynamic sort, instead of relying on the pre-existing mechanisms of the GridView sort routine you would instead implement your own. The sort would still be dynamic to the GridView however because it is still just propagating which public property it would like its data sorted on.
NickLarsen