RadGrid Date Sort not Working

2 Answers 42 Views
Grid
Rakhee
Top achievements
Rank 1
Iron
Iron
Iron
Rakhee asked on 12 Dec 2024, 11:30 AM

Hi

I have a Telerik rad grid which populates data, one of the columns is date and is in format of DD-MMM-YY.

when I click on the header of the column to sort, it doesnt sort it correct in ascending date or descending order. Im assuming its because of the format as the month is a string.

Can anyone advise how to get it to sort correctly please?

Thanks

Rakhee

2 Answers, 1 is accepted

Sort by
1
Rakhee
Top achievements
Rank 1
Iron
Iron
Iron
answered on 08 Jan 2025, 12:26 PM

This was a data issue.

Managed to get round the sorting issue by adding switch statement to the radGrid_sortcommand

0
Rumen
Telerik team
answered on 16 Dec 2024, 09:18 AM

Hello,

To ensure that the RadGrid sorts the date column correctly, you need to make sure that the data is treated as a DateTime object rather than a string. Here are some steps to help you achieve this:

  • Ensure Correct Data Type: Verify that the data source for the date column is of type DateTime. If you are retrieving data from a database, ensure that the date field is of a date type (e.g., DateTime, smalldatetime) and not a string.

  • Set the DataType Property: In your RadGrid configuration, ensure that the DataType property of the column is set to System.DateTime. This informs the grid to treat the field as a date.

    <telerik:GridDateTimeColumn UniqueName="Date" DataField="Date" HeaderText="Date" AllowSorting="true" ShowSortIcon="true" DataType="System.DateTime" DataFormatString="{0:dd-MMM-yy}" ReadOnly="true">
    </telerik:GridDateTimeColumn>
    
  • Data Formatting: Use the DataFormatString property to display the date in the desired format (DD-MMM-YY), but ensure that the underlying data remains a DateTime object.

  • Convert String to DateTime: If your data source provides the date as a string, you need to convert it to DateTime before binding it to the grid. You can do this in your data access logic or directly in the NeedDataSource event.

    Example of converting a string to DateTime in the NeedDataSource event:

    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dynamic data = new[] {
            new { ID = 1, Name = "Name1", Date = DateTime.ParseExact("22-Jan-14", "dd-MMM-yy", null) },
            new { ID = 2, Name = "Name2", Date = DateTime.ParseExact("23-Jan-14", "dd-MMM-yy", null) }
            // Add more data here
        };
        RadGrid1.DataSource = data;
    }
    
    For safer conversion, use DateTime.TryParseExact to handle invalid date strings gracefully.

You can find more information in these forum discussions:

By ensuring that the date data is handled as a DateTime type, the RadGrid should be able to sort the column correctly. If you continue to experience issues, please provide additional details about your data source and grid configuration for further assistance.

 

    Regards,
    Rumen
    Progress Telerik

    Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
    Rakhee
    Top achievements
    Rank 1
    Iron
    Iron
    Iron
    commented on 07 Jan 2025, 01:30 PM | edited

     

    Thank you for your response.

    I have added the DateType property to datetime and the formatstring for the column. I have also made sure the datatype is correct at datetime.

    When I click to sort ascending it works fine but when click on descending it changes all the dates in that column to 01-Jan-01, where as before sorting the dates ranged from 16-Feb-2024 to 19-Feb-2026. Now there all say 01-Jan-01

    Thanks

    Rakhee

    Tags
    Grid
    Asked by
    Rakhee
    Top achievements
    Rank 1
    Iron
    Iron
    Iron
    Answers by
    Rakhee
    Top achievements
    Rank 1
    Iron
    Iron
    Iron
    Rumen
    Telerik team
    Share this question
    or