Question

This is my first real question of need for any of those Gridview experts out there in the .NET world.

I an creating a Gridview from codebehind and I am holding a bunch of numerical data in the columns. Although, I do add the comma in the number fields from codebehind. When I load it to the Gridview, I have the sorting ability turned on, BUT the gridview chooses to ALPHA sort rather than sorting numerically because I add in those commas.

So I need help. Anyone willing to give this one a shot? I need to change some of my columns in the gridview to numerical sort rather than the alpha sort it is using.

Was it helpful?

Solution 4

Instead, I just resorted to the JQUERY Table Sorter.

can be found here: tablesorter

OTHER TIPS

If you do end up implementing your own comparer and sorting them as strings, the algorithm for treating numbers 'properly' is called Natural Sorting. Jeff wrote a pretty good entry on it here:
Sorting for Humans : Natural Sort Order

You can find a pretty good implementation in C# here:
http://www.codeproject.com/KB/string/NaturalSortComparer.aspx

Depending on exactly how you are doing sorting you could use one of the above methods, or you could return to the DB and get the sorting done there if the columns are actually a number type, then add your decoration to it later.

P-Invoke is your friend.

[DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)]
private static extern int StrCmpLogicalW(string psz1, string psz2);

Then you could use it as your own comparer.

For example (in VS2005),

Array.Sort(tringArray, delegate(string left, string right)
{
    return StrCmpLogicalW(left, right);
});

I realize this is really old, but you're mixing data with presentation; that's what's screwing up the sort. Get the number out of SQL without adding commas, then add them in the presentation layer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top