none
custom dataview sorting RRS feed

  • Question

  • Hello,

     

    Is there a way to perform a custom sort using a Dataview. I have a datatable that has a field that contains hours in the format 12:00 PM, when I sort by this column the data is sorted as strings. I would like to be able to sort it as time.

     

    The datagridview I am using to populate the data is databound, so I cannot use the SortCompare event to custom sort the data. For the same reason I cannot use the DataGridView.Sort(IComparer) method to sort the data manually.

     

    Is there a solution for this scenario? Do I have to build my own, custom DataView to be able to handle the sorting manually or I have to change the schema of the DataTable to address this issue?

     

    Any ideas are greatly appreciated. Thank you,

     

    Daniel

     

     

    Saturday, June 28, 2008 7:01 PM

Answers

  • If column is declared as string, then it will be stored as string. You have several options:

     

    1. Change data type of this column to DateTime. Then all the values will be sorted as dates
    2. You could create additional expression column in DataTable of DateTime data type and use Expression property of this column to convert string values of existing column into DateTime, something like

     

    MyNewColumn.Expression = "Convert(MyTimeColumnNameHere, 'System.DateTime')

     

    In both cases you would need to use DataView to get sorted result

     

    Tuesday, July 1, 2008 2:25 AM
    Moderator

All replies

  • If column is declared as string, then it will be stored as string. You have several options:

     

    1. Change data type of this column to DateTime. Then all the values will be sorted as dates
    2. You could create additional expression column in DataTable of DateTime data type and use Expression property of this column to convert string values of existing column into DateTime, something like

     

    MyNewColumn.Expression = "Convert(MyTimeColumnNameHere, 'System.DateTime')

     

    In both cases you would need to use DataView to get sorted result

     

    Tuesday, July 1, 2008 2:25 AM
    Moderator
  • Hi Val,

     

    Thank you for your reply. I ended up doing exactly what you suggested. I created another column that I uses the time from the first column and then I sorted the Dataview by this column. That worked well, and it was much easier to implement than to create my own Dataview class.

     

    Thank you for your help,

    Daniel

     

     

     

    Tuesday, July 1, 2008 8:16 PM