对数据表中的行进行排序 [英] Sorting rows in a data table
问题描述
我们在 DataTable
中有两列,如下所示:
We have two columns in a DataTable
, like so:
COL1 COL2
Abc 5
Def 8
Ghi 3
我们正在尝试根据 COL2
按降序对这个 datatable
进行排序.
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 = "COL2 desc";
ft = ft.DefaultView.ToTable(true);
但是,在不使用 DataView
的情况下,我们希望对 DataTable
本身进行排序,而不是对 DataView
进行排序.
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.
您可以做的是从您从原始 DataTable 创建的 DataView 创建一个新的 DataTable.在 DataView 上应用您想要的任何排序和/或过滤器,然后使用 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屋!