locked
Sorting a datatable by date RRS feed

  • Question

  • User813105968 posted

    Hi,

    Im trying to sort my datatable according to one column containing dates. The datatable is filled by a query to my database and because of this the column that needs the sort is of type system.string. Also, I can't sort in my query because i am combining two datatables together. This makes two different datatables sorted seperatly in one datatable and I dont want that. 

    Im using this date format in my query:

    DAY(DateTravail) & ""/"" & MONTH(DateTravail) & ""/"" & YEAR(DateTravail) AS [Date Travail]

    So how can I sort my datatable according to my date column?

     

    Wednesday, May 4, 2011 8:56 AM

Answers

  • User1508394307 posted

    if you add a new column with a datetime data type and copy all values from DateTravail and sort it by that new column, would it work?

    something like this

    DataTable table = new DataTable();
    ...
    table.Columns.Add("DateTravail2", typeof(DateTime));
    
    foreach (DataRow r in table.Rows)
    {
    r["DateTravail2"] = DateTime.Parse(r["DateTravail"].ToString());
    }
    	
    DataView dv = table.DefaultView;
    dv.Sort = "DateTravail2";
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 4, 2011 9:40 AM

All replies

  • User1508394307 posted

    if you add a new column with a datetime data type and copy all values from DateTravail and sort it by that new column, would it work?

    something like this

    DataTable table = new DataTable();
    ...
    table.Columns.Add("DateTravail2", typeof(DateTime));
    
    foreach (DataRow r in table.Rows)
    {
    r["DateTravail2"] = DateTime.Parse(r["DateTravail"].ToString());
    }
    	
    DataView dv = table.DefaultView;
    dv.Sort = "DateTravail2";
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 4, 2011 9:40 AM
  • User813105968 posted

    Cheers! Thats just what I needed.

    Wednesday, May 4, 2011 10:29 AM