asp.net ListView进行数字排序 [英] asp.net ListView sorting numerically
问题描述
我有一个绑定到DataTable源的列表视图.排序工作,有点.
I have a list view which is bound to a DataTable source. The sorting works, kinda.
对于文本来说很好,但对于数字来说则不是很多.
For text is fine but not so much for numbers.
例如,如果我对1-12进行降序排序,则得到9,8,7,6,5,4,3,2,12,11,10,1.
For example if I sort 1-12 descending I get 9,8,7,6,5,4,3,2,12,11,10,1.
如何获得正确的顺序?
我正在使用:
lvPos.Sort("Position", SortDirection.Descending);
推荐答案
您可能必须自己进行排序,而不是依赖ListView的sort方法.
You're likely going to have to do the sorting yourself instead of relying on ListView's sort method.
如果仅使用代码进行排序,这非常容易.只需将DataTable推入DataView并使用DataView的Sort方法即可.然后绑定到您的DataView而不是您的DataTable.
If you're sorting just using code, this is very easy. Just shove your DataTable into a DataView and use the DataView's Sort method. Then bind to your DataView instead of your DataTable.
var view = new DataView(dtTable);
view.Sort = "Position"; //or "Position DESC"
lvPos.DataSource = view;
lvPos.DataBind(); //use lvPos.DataBind(true) to raise the "OnDataBinding" event.
如果您要通过ListView的sort方法进行排序(即,页面中有一些东西触发了ListView.Sorting/Sorted事件),则您必须挂接到Sorting事件并取消它.继续使用之前将DataTable推入DataView(或重新使用DataView!),按新表达式排序并重新绑定数据的技术.
If you're sorting from the ListView's sort methods (i.e. you have something in the page triggering the ListView.Sorting/Sorted events), then you would have to hook on to the Sorting event and cancel it. Follow up with the previous technique of shoving the DataTable into a DataView (or re-use the DataView!), sort by the new expression, and rebind your data.
private void lvPos_Sorting(object sender, ListViewSortEventArgs args)
{
var view = new DataView(dtTable);
view.Sort = args.SortExpression;
lvPos.DataSource = view;
lvPos.DataBind();
args.Cancel = true;
}
这篇关于asp.net ListView进行数字排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!