C# 动态数据表按升序或降序排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17571903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dynamic datatable sorting in ascending or descending
提问by Mitesh Jain
I have created dynamic table
我创建了动态表
DataTable date = new DataTable();
date.Columns.Add("date1");
and made fill the column name "date1" with date as
并用日期填充列名“date1”
date1(Column name)
05-07-2013
10-07-2013
09-07-2013
02-07-2013
and made fill my dynamic table
并填满我的动态表
Now i want this dynamic table data to be sort as ascending or descending order
现在我希望这个动态表数据按升序或降序排序
For eg:
date1(Column name)
02-07-2013
05-07-2013
09-07-2013
10-07-2013
采纳答案by Andrei
This cannot be done with the original data table. However you can create a new, sorted one:
这不能用原始数据表完成。但是,您可以创建一个新的、排序的:
DataView view = date.DefaultView;
view.Sort = "date1 ASC";
DataTable sortedDate = view.ToTable();
回答by Soner G?nül
You can use DataTable.Select(filterExpression, sortExpression)method.
您可以使用DataTable.Select(filterExpression, sortExpression)方法。
Gets an array of all DataRow objects that match the filter criteria, in the specified sort order.
按指定的排序顺序获取与筛选条件匹配的所有 DataRow 对象的数组。
date.Select("", "YourColumn ASC");
or
或者
date.Select("", "YourColumn DESC");
As an alternative, you can use DataViewlike;
作为替代方案,您可以使用DataViewlike;
DataView view = date.DefaultView;
view.Sort = "YourColumn ASC";
DataTable dt = view.ToTable();
回答by Johnathan Brown
Thought I would give in my two cents here. Instead of using a sorting algorithm which takes time and computational performance, why not instead reverse the way in which you are adding data to your data object.
以为我会在这里付出我的两美分。与其使用耗费时间和计算性能的排序算法,不如改变向数据对象添加数据的方式。
This won't work for everyone's scenario - but for my own it worked out perfectly.
这不适用于每个人的场景 - 但对于我自己的场景来说效果很好。
I had a database which listed items in an ascending order, but or ease of use I needed to reverse the way in which people could see the data (DESC) so that the newest input shows at the top, rather then the bottom of the list.
我有一个按升序列出项目的数据库,但为了便于使用,我需要颠倒人们查看数据的方式 (DESC),以便最新输入显示在顶部,而不是列表底部.
So, I just changed my for loop so instead of working from 0 -> upwards, it started from the length of the datatable (-1 to stop an overflow) and then stops when it is >= to 0;
所以,我只是改变了我的 for 循环,而不是从 0 -> 向上工作,它从数据表的长度开始(-1 停止溢出),然后在 >= 到 0 时停止;
private Dictionary<string, string> GetComboData(string table, int column, bool id, int idField = 0)
{
SqlClass sql = new SqlClass(database);
Dictionary<string, string> comboBoxData = new Dictionary<string, string>();
if (sql.connectedToServer)
{
sql.SelectResults(SQLCommands.Commands.SelectAll(table));
for (int i = sql.table.Rows.Count-1; i >= 0; i--)
{
string tool = sql.table.Rows[i].ItemArray.Select(x => x.ToString()).ToArray()[column];
string ID = sql.table.Rows[i].ItemArray.Select(x => x.ToString()).ToArray()[idField];
comboBoxData.Add(ID, tool);
}
}
return comboBoxData;
}
回答by Harshali Jadhav
using OrderByDescending()
使用 OrderByDescending()
@foreach (var rca in Model.OrderByDescending(x=>x.Id))
{
<tr class="heading">
<td>@rca.PBINo</td>
<td>@rca.Title</td>
<td>@rca.Introduction</td>
<td>@rca.CustomerImpact</td>
<td>@rca.RootCauseAnalysis</td>
</tr>
}

