Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTable.DefaultView.Sort Doesn't Sort

Tags:

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.

like image 443
Mike Wills Avatar asked Jul 30 '09 18:07

Mike Wills


2 Answers

actionLogDT.DefaultView.Sort = "StartDate";

actionLogDT = actionLogDT.DefaultView.ToTable();
like image 135
ashdiggedy Avatar answered Oct 31 '22 09:10

ashdiggedy


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
}
like image 23
Jeromy Irvine Avatar answered Oct 31 '22 11:10

Jeromy Irvine