DataTable的行按列值排序 [英] DataTable`s rows sorting by value of a column

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

问题描述

您好,我使用此代码按列中的值对表的行进行排序,但它并没有执行我想要的操作,例如,应该对行进行如下排序:
0; 0.1; 1; 2; .... 9; 10,但是我的代码排序如下:0; 0.1; 1; 10; 2; ... 9;因此,它还有其他方法可以正确地进行这种排序(行的排序必须按列中的值升序排列),或者有人可以告诉我我在哪里搞砸了?

Hello, i use this code to sort a table`s rows by values from a column but it doesnt do what i want for example it should sort the rows like this:
0; 0.1;1;2;....9;10 but my code sort`s like this : 0;0.1;1;10;2;...9; So its there any other way to do this sorting right(the sort of rows must be ascending by values from a column) or can someone tell me where i messed up?

public static DataTable sort(DataTable dt)
      {


         dt.DefaultView.Sort="["+dt.Columns["Distanta Euclidiana"]+"] asc";

          return dt;
      }

推荐答案

我认为要排序的column Data typestring,在这种情况下,如果按升序排序,则为10由于排序是从左到右进行的,因此它位于2之前,而字符"1"则位于字符"2"之前.
如果将列的数据类型设置为numeric types中的任何一个,例如double,则可以实现指定的所需排序.
I think the Data type of the column being sorted is string, in which case, if sorted in ascending order, 10 comes before 2 as the sorting is done from the left to right and character ''1'' comes before the character ''2''.
If the data type of column is set to any of numeric types say double, then the desired sorting as specified can be achieved.


这篇关于DataTable的行按列值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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