简体   繁体   English

.Net Gridview Alpha排序,需要进行数字排序

[英].Net Gridview alpha sorting, it needs to be numerically sorted

This is my first real question of need for any of those Gridview experts out there in the .NET world. 这是我的第一个真正的问题,即.NET世界中是否需要任何Gridview专家。

I an creating a Gridview from codebehind and I am holding a bunch of numerical data in the columns. 我从后台代码创建一个Gridview,并且在列中保存了一堆数字数据。 Although, I do add the comma in the number fields from codebehind. 虽然,但我确实在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. 当我将其加载到Gridview时,我启用了排序功能,但由于我添加了这些逗号,因此gridview选择了ALPHA排序而不是数字排序。

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. 我需要将gridview中的某些列更改为数字排序,而不是它使用的alpha排序。

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: 您可以在C#中找到一个很好的实现:
http://www.codeproject.com/KB/string/NaturalSortComparer.aspx http://www.codeproject.com/KB/string/NaturalSortComparer.aspx

Instead, I just resorted to the JQUERY Table Sorter. 相反,我只是诉诸于JQUERY表排序器。

can be found here: tablesorter 可以在这里找到: tablesorter

根据确切的排序方式,您可以使用上述方法之一,或者如果列实际上是数字类型,则可以返回到DB并在那里完成排序,然后再添加修饰。

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. 从SQL中获取数字而不添加逗号,然后将其添加到表示层中。

P-Invoke is your friend. P-Invoke是您的朋友。

[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), 例如(在VS2005中),

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM