C# DataTable.DefaultView.Sort 不排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1208548/
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
DataTable.DefaultView.Sort Doesn't Sort
提问by Mike Wills
I am confused on DataTable.DefaultView.Sort. Here is the segment of the code I want to use it in.
我对 DataTable.DefaultView.Sort 感到困惑。这是我想在其中使用它的代码段。
actionLogDT.DefaultView.Sort = "StartDate";
foreach (CustomerService.ActionLogStartEndRow logRow in actionLogDT)
{
// code here
}
The samples I have seen don't use the foreach loop and thus is confusing me on how to process this. It isn't sorting as I thought it should be.
我看到的示例没有使用 foreach 循环,因此让我对如何处理这个循环感到困惑。它不像我认为的那样排序。
I see that .DefaultView returns a view, and .Table gives a compile error.
我看到 .DefaultView 返回一个视图,而 .Table 给出一个编译错误。
采纳答案by Mike Wills
I had to take a slightly different approach. This postwas the closest I could find to get my code to work. Here is the working result:
我不得不采取稍微不同的方法。这篇文章是我能找到的最接近让我的代码工作的帖子。这是工作结果:
actionLogDT.DefaultView.Sort = "StartDate";
DataView dv = actionLogDT.DefaultView;
foreach (DataRowView logRow in dv) { . . . }
From there I just have to cast the value back into it's proper type.
从那里我只需要将值转换回它的正确类型。
(string)logRow["Status"].ToString()
回答by Jeromy Irvine
Sorting the view won't change the sort order of the data in the table, just the order in the view. It should work if you do your foreach
on the view instead, casting the row from the DataRowView back to your strongly typed row.
对视图进行排序不会改变表中数据的排序顺序,只会改变视图中的顺序。如果您foreach
在视图上执行您的操作,它将行从 DataRowView 转换回强类型行,它应该可以工作。
foreach (DataRowView logRowView in actionLogDT.DefaultView)
{
CustomerService.ActionLogStartEndRow logRow = logRowView.Row as CustomerService.ActionLogStartEndRow;
// code here
}
回答by John Sheehan
foreach (var logRow in actionLogDT.DefaultView.ToDataTable()) { ... }
回答by jp2code
Just curious: Why are you using the DataRowView?
只是好奇:你为什么使用DataRowView?
i.e.
IE
foreach (DataRow row in actionLogDT.Rows)
{
Console.WriteLine(row["Status"]);
}
回答by Upender
Please try this:
请试试这个:
actionLogDT.DefaultView.Sort = "["+actionLogDT.Columns[0].ColumnName+"] asc";
回答by ashdiggedy
actionLogDT.DefaultView.Sort = "StartDate";
actionLogDT = actionLogDT.DefaultView.ToTable();
回答by Adi Bilauca
If you need a datatable back then you can do something like:
如果您需要一个数据表,那么您可以执行以下操作:
var dv = actionLogDT.DefaultView;
dv.Sort = "StartDate";
actionLogDT = dv.ToTable();
回答by Kevin Cain
Additionally, Since it seemed like you wanted to loop through records, you can just loop through the dataRowView
objects in the DefaultView
.
此外,由于这似乎是你通过记录要循环,你可以通过循环dataRowView
中的对象DefaultView
。
foreach (DataRowView drv in table.DefaultView)
{
string strValue = drv["ColumnName"].ToString();
// its also worth mentioning that a DataRowView has a Row
strValue = drv.Row["ColumnName"].ToString();
}