tags:

views:

1043

answers:

7

Hey guys,

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.

A: 

I believe you can provide your own comparer object for sorting of a gridview. Just impliment IComparer. Here is an article:

Gridview Guy Article

Jason Jackson
+1  A: 

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:

http://www.codinghorror.com/blog/archives/001018.html

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

Travis
A: 

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.

Mitchel Sellers
A: 

@All, Thanks guys. I am looking through the posts and will get back to you.

Scott
A: 

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);
});
TonyOssa
A: 

@TonyOssa,

How do I apply this to my gridview though?

Scott
A: 

Instead, I just resorted to the JQUERY Table Sorter.

can be found here: tablesorter

Scott