排序数据表 [英] Sorting a Data Table
问题描述
我想一个数据表进行排序有以下两种方式。
I tried to sort a data table with following two ways
table.DefaultView.Sort = "Town ASC, Cutomer ASC"
table.Select("", "Town ASC, Cutomer ASC")
但他们没有没有奏效。它始终显示在原来的订单数据。你有任何想法来解决这个问题。
But none of them wasn't worked. It always displays data in original order. Do you have any idea to solve the problem.
推荐答案
设置排序前pression的默认视图( table.DefaultView.Sort =镇ASC,ASC CUTOMER后
),你应该在循环使用的默认视图而不是DataTable实例本身的表
After setting the sort expression on the DefaultView (table.DefaultView.Sort = "Town ASC, Cutomer ASC"
) you should loop over the table using the DefaultView not the DataTable instance itself
foreach(DataRowView r in table.DefaultView)
{
//... here you get the rows in sorted order
Console.WriteLine(r["Town"].ToString());
}
使用DataTable的选择方法来代替,产生的DataRow数组。这个数组排序从你的要求,而不是数据表
Using the Select method of the DataTable instead, produces an array of DataRow. This array is sorted as from your request, not the DataTable
DataRow[] rowList = table.Select("", "Town ASC, Cutomer ASC");
foreach(DataRow r in rowList)
{
Console.WriteLine(r["Town"].ToString());
}
这篇关于排序数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!