DataTable.DefaultView.Sort Doesn't Sort
Solution 1
I had to take a slightly different approach. This post was 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()
Solution 2
actionLogDT.DefaultView.Sort = "StartDate";
actionLogDT = actionLogDT.DefaultView.ToTable();
Solution 3
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 logRowView in actionLogDT.DefaultView)
{
CustomerService.ActionLogStartEndRow logRow = logRowView.Row as CustomerService.ActionLogStartEndRow;
// code here
}
Solution 4
foreach (var logRow in actionLogDT.DefaultView.ToDataTable()) { ... }
Solution 5
Additionally, Since it seemed like you wanted to loop through records, you can just loop through the dataRowView
objects in the 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();
}
Mike Wills
After going to school to become a network administrator, Mike, fell in love with programming. He has been a midrange (IBM i, iSeries, AS/400) developer since 2000. He has since added ASP.NET to to his toolbox (around 2006) and now does dual duty writing/supporting green-screen applications and ASP.NET internet and intranet applications. He does a lot of integration between the IBM i and ASP.NET. Besides his programming day job, he dabbles in PHP and many other areas of tech. You can find him on his website, @MikeWills, Facebook, and Google+
Updated on July 09, 2022Comments
-
Mike Wills almost 2 years
I am confused on DataTable.DefaultView.Sort. Here is the segment of the code I want to use it in.
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.
I see that .DefaultView returns a view, and .Table gives a compile error.
-
Mike Wills almost 15 yearsDoing that gives me an "Unable to cast object of type 'System.Data.DataRowView' to type 'ActionLogStartEndRow'." error.
-
Jeromy Irvine almost 15 yearsDoes
ActionLogStartEndRow
derive from DataRow? -
Mike Wills almost 15 years@Jeromy - I am sure it does. I am using the .XSD data layer to accomplish all of this.
-
Mike Wills almost 15 yearsBasically the same casting error. Unable to cast object of type 'System.Data.DataRow' to type 'ActionLogStartEndRow'.
-
Jeromy Irvine almost 15 yearsOK, I updated the answer to account for the strongly typed row. This should hopefully do the trick for you.
-
Mike Wills over 14 yearsI don't understand your question.
-
Mike Wills about 14 yearsI'll try that next time I have this problem.
-
Stuart Hallows about 14 yearsBecause the question was about sorting a datatable. To enumerate the sorted data you need to enumerate the view, not the table itself.
-
Lukas over 11 yearsThank you! Just what I needed. Get a table, sort it, and use it while it's sorted a certain way.
-
vapcguy over 7 yearsCast is redundant...
.ToString()
should be enough. -
storsoc about 7 yearsSlightly different scenario/symptoms for me, but the .toTable() solved this for me. Thanks. In my case, I was applying the DefaultView.Sort expression just fine and seeing the sort take effect for DropDownList, but it wasn't taking effect for a DataGrid. YMMV: The DataGrid we're applying this to is a very basic read-only table that does not provide dynamic sorting, etc.
-
FredyWenger about 5 yearsI have ported code from vb.net (with DT.DefaultView.Sort) to c# asp-net core. In vb.net, the sort has worked without overwrite (.DefaultView.ToTable();). Your posting here did the trick - thanks!