This is a migrated thread and some comments may be shown as answers.

RadGrid case-insensitive sort?

1 Answer 198 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
KN425000
Top achievements
Rank 1
KN425000 asked on 23 Mar 2011, 03:52 PM


I have a RadGrid bound to an IList<SomeType>, where SomeType is defined as a struct with two string properties (e.g., A and B).  In the Grid, column 0 displays property A and column 1 displays property B.   Both properties contain data in mixed case.

The grid sorts the data in a case-sensitive manner, such that "AA" is the first item and "zz" is the last item, and "Music" and "music" are not next to each other:

Is there a way to make the grid perform the sort in a case-insensitive manner?  How do people normally handle this situation with the RadGrid?   Do I need to do a case-insensitive sort of the data and rebind (AKA "custom sorting")?

Thanks!

1 Answer, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 24 Mar 2011, 03:55 PM
Hi Mike,

The grid itself uses CompareTo function (that is introduced in the IComparable interface) for sorting.
This function for the System.String is like:
public static int Compare(string strA, string strB)
{
    return CultureInfo.CurrentCulture.CompareInfo.Compare(strA, strB, CompareOptions.None);
}
So the grid uses indirectly the CurrentCulture when sorting a column with string values.
If you use en-US culture for example, the sorting will be case-insensible.
For information how to change the current culture, see this documentation: http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

Changing the culture will also mean different date/time format, currency, etc. If you want to use your current culture, but with compare function from the "en-US" culture, you can make your custom culture:
public class MyCultureInfo : CultureInfo
{
    public MyCultureInfo(string name)
        : base(name)
    {
    }
    public override CompareInfo CompareInfo
    {
        get
        {
            return (new CultureInfo("en-US")).CompareInfo;
        }
    }
}

Then in Page_Load to set it as CurrentCulture for your page:
Thread.CurrentThread.CurrentCulture = new MyCultureInfo(Thread.CurrentThread.CurrentCulture.Name);

Please see the attached website for a working example of this approach.

Another approach will be to use your custom sorting. Check this online demo: http://demos.telerik.com/aspnet-ajax/grid/examples/programming/sort/defaultcs.aspx. You can handle SortCommand event of the grid like it is done in the demo. Then sort the list of objects by your wish and bind the grid to the sorted list.

Greetings,
Vasil
the Telerik team
Tags
General Discussions
Asked by
KN425000
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Share this question
or