排序数据表不起作用 [英] Sorting the DataTable is not working

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

问题描述

我有一个名为 DTStage的数据表。它有两列,例如 SeqNbr和 StageID。



我想基于 SeqNbr对数据表进行排序。



DataTable具有以下记录:

  SeqNbr StageID 
------- ------------
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
18 18
17 19

我使用了两种方法对该数据表进行排序,如下所示:

  DTStage = new DataView( DTStage,, SeqNbr asc,DataViewRowState.CurrentRows).ToTable(); 

&

DataView dv = DTStage.DefaultView;
dv.Sort = DTStage.Columns [ SeqNbr] + asc;
DTStage = dv.ToTable();

但是两者给出的结果相同:

  SeqNbr StageID 
-------- -----------
0 1
1 2
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 19
18 18
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

为什么这不能正常工作?

解决方案

它看起来像您的SeqNbr列是字符串类型的,所以它按字符而不是按数字。如果这些是数字(我假设是整数),为什么在DataTable中不将列类型设置为int?



这应该可以帮助您:使用c#将数据表中的字符串项按int排序 p>

I have one DataTable called "DTStage". It has two columns such as "SeqNbr" and "StageID".

I want to sort the datatable based on "SeqNbr".

The DataTable has the following records:

     SeqNbr         StageID
    --------      -----------
       0               1
       1               2
       2               3
       3               4
       4               5
       5               6
       6               7
       7               8
       8               9
       9               10
       10              11
       11              12
       12              13
       13              14
       14              15
       15              16
       16              17
       18              18
       17              19

I have used two methods to sort this datatable, they are as follows:

 DTStage = new DataView(DTStage, "", "SeqNbr asc", DataViewRowState.CurrentRows).ToTable();

                     &

 DataView dv = DTStage.DefaultView;
 dv.Sort = DTStage.Columns["SeqNbr"] + " asc";
 DTStage = dv.ToTable();

But both gives the same result as follows:

     SeqNbr         StageID
    --------      -----------
        0               1
        1               2
       10               11
       11               12
       12               13
       13               14
       14               15
       15               16
       16               17
       17               19
       18               18
        2               3
        3               4
        4               5
        5               6
        6               7
        7               8
        8               9
        9               10

Why this is not working properly? How to solve this problem?

解决方案

it looks like your SeqNbr column is of type string so it gets sorted by characters and not as numbers. if those are numbers (I assume integers) why don't you have the column type as int in the DataTable?

This should help you: Sort string items in a datatable as int using c#

这篇关于排序数据表不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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