在一个数据表的排序的行 [英] Sorting rows in a data table
问题描述
我们有两列在数据表
,就像这样:
We have two columns in a DataTable
, like so:
COL1 COL2
Abc 5
Def 8
Ghi 3
我们正在努力解决这数据表
根据 COL2
的递减顺序。
We're trying to sort this datatable
based on COL2
in decreasing order.
COL1 COL2
ghi 8
abc 4
def 3
jkl 1
我们尝试这样做:
ft.DefaultView.Sort = "occr desc";
ft = ft.DefaultView.ToTable(true);
但是,如果没有使用数据视图
,我们要排序的数据表
本身,而不是数据视图
。
but, without using a DataView
, we want to sort the DataTable
itself, not the DataView
.
推荐答案
我怕你不能轻易做一个就地排序的DataTable像它听起来就像你想要做的。
I'm afraid you can't easily do an in-place sort of a DataTable like it sounds like you want to do.
你可以做的是创建您从原始数据表中创建一个DataView一个新的DataTable。适用于任何你想要的排序和/或过滤器的数据视图,然后使用创建从数据视图一个新的DataTable的<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.dataview.totable.aspx\">DataView.ToTable方法:
What you can do is create a new DataTable from a DataView that you create from your original DataTable. Apply whatever sorts and/or filters you want on the DataView and then create a new DataTable from the DataView using the DataView.ToTable method:
DataView dv = ft.DefaultView;
dv.Sort = "occr desc";
DataTable sortedDT = dv.ToTable();
这篇关于在一个数据表的排序的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!