DATA VIEW SORT中的数字排序 [英] Numeric sorting in DATA VIEW SORT

查看:85
本文介绍了DATA VIEW SORT中的数字排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI All,

我想在升序或降序中对DATA VIEW SORT的列进行排序。

我的现有代码适用于字符串排序,但是当涉及到数字排序时它会做错误的。

例如,

当我排序时列号按升序排列,有数字,类似于,

1

12

123



2

23

234



3

34

345



但是我想要这个订单

1

2

3

12

23

34

123

245等。



有谁可以帮我这个?

HI All,
I want to sort the columns of the DATA VIEW SORT in Ascending or Descending.
My Existing code works fine for string sorting but when it comes to numeric sorting it does wrongly.
For Example,
When I sort a column in ascending order which has numbers, it sorts like,
1
12
123

2
23
234

3
34
345

But I want in this order
1
2
3
12
23
34
123
245 etc .

Can anyone help me to this?

推荐答案

您可以通过以下方式创建DataTable克隆,并将列的数据类型更改为Int。



DataTable dt = GetTable(); //假设此方法从服务返回数据表

DataTable dt2 = dt.Clone();

dt2.Columns [Code]。DataType = Type.GetType( System.Int32);



foreach(DataRow dr in dt.Rows)

{

dt2 .ImportRow(dr);

}

dt2.AcceptChanges();

DataView dv = dt2.DefaultView;

dv.Sort =Code ASC;
You can create DataTable via Clone, and change the data type of the column to Int.

DataTable dt = GetTable(); // Assume this method returns the datatable from service
DataTable dt2 = dt.Clone();
dt2.Columns["Code"].DataType = Type.GetType("System.Int32");

foreach (DataRow dr in dt.Rows)
{
dt2.ImportRow(dr);
}
dt2.AcceptChanges();
DataView dv = dt2.DefaultView;
dv.Sort = "Code ASC";


这篇关于DATA VIEW SORT中的数字排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆