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

问题描述

我尝试使用以下两种方式对数据表进行排序

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.

推荐答案

在 DefaultView (table.DefaultView.Sort = "Town ASC, Cutomer ASC" ) 上设置排序表达式后,您应该循环在表格上使用 DefaultView 而不是 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 的 Select 方法,生成一个 DataRow 数组.此数组按您的请求排序,而不是 DataTable

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());
}

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

08-05 00:14