GridDateTimeColumn date column sort

I create a date column in my grid and define it with "GridDateTimeColumn",


The original data is formatted like this "dd/MM/yyyy", and when I sort the column in ascending or descending order, it filters by days and not by early/late date,


That is, if I have these 2 dates,


12/02/2022

11/06/2022


So the second date will appear first in the sort in ascending order, even though its date is later..


I tried converting the strings to a date object before loading, but that didn't help


                             foreach (System.Data.DataRow row in dataTable.Rows)

                             {

                                 if (!string.IsNullOrEmpty(row["entryDate"].ToString()))

                                 {

                                     DateTime entryDateTime = DateTime.ParseExact(row["entryDate"].ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);

                                     row["entryDate"] = entryDateTime;

                                 }


                                 if (!string.IsNullOrEmpty(row["departureDate"].ToString()))

                                 {

                                     DateTime departureDateTime = DateTime.ParseExact(row["departureDate"].ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);

                                     row["departureDate"] = departureDateTime;

                                 }



                             }


                             dataView = new DataView(dataTable);

                             dataGrid.ItemsSource = dataView;



I would appreciate your help :)


1 Reply

SB Sweatha Bharathi Syncfusion Team March 13, 2024 02:51 PM UTC

Hi Eyal_Gilboa ,



We have analyzed your query. We have checked your reported scenario where the GridDateTimeColumn does not sort based on dates. When we perform sorting, it sorts based on the dates themselves, not their values. It is working fine as expected.


We suspect that you are using a DataTable, and you may have set the data type as string for the DateTime column. Can you confirm whether you have set the data type as string for the DateTime column? In order to sort based on dates, you need to set the data type as DateTime.


We have provided a sample for your reference. Kindly refer to the sample and let us know if you have any further concerns regarding this issue.


Code Snippet:


   private DataTable GetDataTable()

   {

       // Create a new DataTable

       DataTable dataTable = new DataTable();

 

       // Add columns to the DataTable

       dataTable.Columns.Add("ID", typeof(int));

       dataTable.Columns.Add("Name", typeof(string));

       dataTable.Columns.Add("Date", typeof(DateTime));

 

       // Add rows to the DataTable

       dataTable.Rows.Add(1, "John", new DateTime(2022,2,12));

       dataTable.Rows.Add(2, "Alice", new DateTime(2022, 4, 15));

       dataTable.Rows.Add(3, "Bob", new DateTime(2022, 6, 11));

 

       return dataTable;

   }




Attachment: Sample_and_Images_74339d23.zip

Loader.
Up arrow icon