Articles in this section
Category / Section

How to group a column with DateTime values without showing its time part in Xamarin.Android DataGrid?

2 mins read

Xamarin.Android DataGrid 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.

Graphical user interface, application

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.

 

GridTextColumn dateColumn = new GridTextColumn ();
dateColumn.MappingName="ShippingDate";
dateColumn.Format = "dd/MM/yyyy";
grid.Columns.Add (dateColumn);

In the following screenshot note that DateTime data in the ShippingDate column contains only the date part.

Graphical user interface

You can see that the group caption summary row contains both the date and time values.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.

MainActivity.cs

grid.GroupColumnDescriptions.Add(newGroupColumnDescription()
{
  ColumnName="ShippingDate",
  Converter=new GroupDateTimeConverter()
});

GroupDateTimeConverter.cs

public class GroupDateTimeConverter:IValueConverter

{ 

   public object Convert (object value, Type targetType, object parameter,CultureInfo culture)

   {

      OrderInfo order = value as OrderInfo;

      return order.ShippingDate.ToString("dd/MM/yyyy");

   }

   public object ConvertBack (object value, Type targetType, object parameter,CultureInfo culture)

   {

      return null;

   }

}

 

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

Graphical user interface, application

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

MainActivity.cs

grid.SortComparers.Add (new SortComparer ()
{ 
   Comparer = new customSortComparer (),
   PropertyName = "ShippingDate" 
});
grid.SortColumnDescriptions.Add (new SortColumnDescription ()
{
   ColumnName = "ShippingDate" 
});

CustomSortComparer.cs

public class CustomSortComparer : IComparer<object>, ISortDirection
{
    public ListSortDirection SortDirection { get; set; }
    public CustomSortComparer()
    {
    }
    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;
    }
}

 

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

Graphical user interface

You can download the working sample for this KB from the below link 


Conclusion

I hope you enjoyed learning about how to group a column with DateTime values without showing its time part in Xamarin.Android DataGrid.

You can refer to our Xamarin.Android DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our  Xamarin.Android DataGrid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied