Hi,
I've a SfDatagrid With a column With date value. When I Group this column in Caption Group the time is also shown, as seen in the image below.
I tried to format the column by converting the date to shortdatestring, but in this way the sorting is wrong.
How can I format the column to display only the date?
Thanks in advance
Hi Maurizio,
We are currently preparing the sample based on your requirements. It will take some time to complete. Therefore, we will provide you with further details on or before April 04, 2024.
We appreciate your patience until then.
Regards,
Sreemon Premkumar M.
Hi Maurizio,
Your requirement to remove the time value in the group caption row can be achieved by setting the GridDateTimeColumn.GroupMode to Display. By doing this, the display text in the column values, which are of type string, will be loaded into the group caption row in the format dd/MM/yyyy. Since the GroupMode is set to Display and the value was loaded as a string, the sorting is based on the text. To sort the group by date, we need to provide a custom comparer to sort the groups in date order. Please find the code snippet below:
Code snippet:
Group mode:
| sfDataGrid1.Columns.Add(new
GridDateTimeColumn() { MappingName = "OrderDate",
HeaderText = "Order Date", GroupMode =
DataReflectionMode.Display}); |
Comparer:
public class CustomComparer : IComparer<object>, Syncfusion.Data.ISortDirection { public CustomComparer() { } public int Compare(object x, object y) { if (x is Group groupvalueX && y is Group groupvalueY) { DateTime dateTimeX = DateTime.ParseExact(groupvalueX.Key.ToString(), "M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture); DateTime dateTimeY = DateTime.ParseExact(groupvalueY.Key.ToString(), "M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture); if (_SortDirection == System.ComponentModel.ListSortDirection.Ascending) return dateTimeX.CompareTo(dateTimeY); else return dateTimeY.CompareTo(dateTimeX); } else { var valueX = (x as OrderInfo).OrderDate; var valueY = (y as OrderInfo).OrderDate; if (_SortDirection == System.ComponentModel.ListSortDirection.Ascending) return valueX.CompareTo(valueY); else return valueY.CompareTo(valueX); }
} private System.ComponentModel.ListSortDirection _SortDirection; /// <summary> /// Gets or sets the property that denotes the sort direction. /// </summary> /// <remarks> /// SortDirection gets updated only when sorting the groups. For other cases, SortDirection is always ascending. /// </remarks> public System.ComponentModel.ListSortDirection SortDirection { get { return _SortDirection; } set { _SortDirection = value; }
|
We have prepared a sample with the implementation, please find it in the attachment.
If you have any queries or difficulties implementing this in your use case, please let us know. We will assist you.
Regards,
Sreemon Premkumar M.