We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to group a column with DateTime values without showing its time part in SfDataGrid?

SfDataGrid control allows you to group a column with DateTime data, however it will display the entire DateTime value including the time part in its cells, like in the following screenshot.

If you do not want to display the time part of the DateTime object then apply Format to the corresponding column. Refer the below code example.

 

<sfgrid:GridTextColumn MappingName="ShippingDate" Format="dd/MM/yyyy" />


In the following screenshot note the DateTime data in the ShippingDate column.



If your requirement is to group by a date column and also to display only the date part in the caption, then you can achieve it by writing a converter to apply the custom grouping logic. Refer the below code example.

 

<sfgrid:SfDataGrid.GroupColumnDescriptions>

<sfgrid:GroupColumnDescription ColumnName="ShippingDate" Converter="{StaticResource groupDateTimeConverter}" />
</sfgrid:SfDataGrid.GroupColumnDescriptions>

 

The following screenshot shows the final outcome upon execution of the above code

Since we have returned a string value in the converter, the groups are sorted considering the GroupKey values as string. If you want to sort the groups considering it to be DateTime value then you need to write a SortComparer for it.

Refer the following code example to create a custom SortComparer

public class CustomSortComparer : IComparer<object>, ISortDirection

{

    #region Property

    public ListSortDirection SortDirection { get; set; }

    #endregion

    #region Constructor

    public CustomSortComparer()

    {

    }

    #endregion

    #region Compare

    public int Compare(object x, object y)

    {

        DateTime dateX = DateTime.MaxValue;

        DateTime dateY = DateTime.MaxValue;

        if (x.GetType() == typeof(OrderInfo))

        {

            dateX = ((OrderInfo)x).ShippingDate;

            dateY = ((OrderInfo)y).ShippingDate;

        }

        else if (x.GetType() == typeof(Group))

        {

            dateX = DateTime.ParseExact(((Group)x).Key.ToString(),

                                                                 "dd/MM/yyyy", CultureInfo.InvariantCulture);

            dateY = DateTime.ParseExact(((Group)y).Key.ToString(),

                                                                "dd/MM/yyyy", CultureInfo.InvariantCulture);

        }

        else

        {

            dateX = (DateTime)x;

            dateY = (DateTime)y;

        }

        if (DateTime.Compare(dateX, dateY) >= 0)

            return SortDirection == ListSortDirection.Ascending ? 1 : -1;

        else

            return SortDirection == ListSortDirection.Ascending ? -1 : 1;

    }

    #endregion

}

 

The following screenshot shows the final outcome without time and proper sort order

You can download the working sample for this KB from the below link
http://www.syncfusion.com/downloads/support/directtrac/general/ze/SortComparer323326929

Article ID: Published Date: Last Revised Date: Platform: Control:
6168 01/27/2016 01/28/2016 Xamarin.Forms SfDataGrid
Tags:
Did you find this information helpful?
Add Comment
You must log in to leave a comment

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon