none
Sort a DataTable by a DataTime column RRS feed

  • Question

  • Code Snippet

    //DataTable dt gets proulated by av stored procedure

    DataView dw = dt.DefaultView;

    dw.Table.Columns["start"].DataType = Type.GetType("System.DateTime");
    dw.Sort = "start ASC";

    foreach (DataRow dr in dw.Table.Rows) {

    System.Diagnostics.Debug.WriteLine("Start: " + dr["start"].ToString());

    }


    The code above is supposed to sort the datatable or view by a datetime column, but it does not work. I have tried different thing, but this is what I tried last.

    I have also see many posts on the forum about the same thing, and they suggest something like this. But as I said it doesn't work.

    If you have a solution thats works, please post the code...

    Anders...
    Tuesday, September 16, 2008 9:53 AM

Answers

  • Result is not physically sorted in DataTable and if you wanr to see sorted result you need to loop through collection of rows in sorted DataView, not in DataTable, something like

     

    foreach (DataRowView dr in dw) {

    System.Diagnostics.Debug.WriteLine("Start: " + dr["start"].ToString());

    }

     

     

     

    Tuesday, September 16, 2008 10:00 AM
    Moderator

All replies

  • Result is not physically sorted in DataTable and if you wanr to see sorted result you need to loop through collection of rows in sorted DataView, not in DataTable, something like

     

    foreach (DataRowView dr in dw) {

    System.Diagnostics.Debug.WriteLine("Start: " + dr["start"].ToString());

    }

     

     

     

    Tuesday, September 16, 2008 10:00 AM
    Moderator
  • YES, thank you very much.

    Anders..
    Tuesday, September 16, 2008 10:59 AM