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. Image for the cookie policy date

Group sorting issu with a DateTimeColumn

Hi,

I have a DateTimeColumn in SfDataGrid and can not properly sort groups, which are made on dates from the column. The column has the Pattern property set to "ShortDate". If the GroupMode is set to "Display", the groups are sorted by string representation of the dates rather than the dates themselves. For example, "01/04/2023" is followed by "01/01/2022", while the expected "31/04/2023" goes down in a list.

The sorting works fine, when GroupMode is set to "Value", but then the group caption shows the full date value as a key, e.g. "01/04/2023 00:00:00"

Is it possible to get the proper date sorting and have a short date in the group caption in the same time?


4 Replies

SJ Sathiyathanam Jeyakumar Syncfusion Team April 25, 2023 01:34 PM UTC

Hi Ilia Korjoukov,

We are currently checking your reported problem with provided information. We will update you with further details on April 27, 2023.

Regards

Sathiyathanam



SJ Sathiyathanam Jeyakumar Syncfusion Team April 26, 2023 03:31 PM UTC

Ilia Korjoukov,

We have analyzed your requirement. When you bind the DateTime in your underlying DataSource, the GroupCaption will show with the DateTime while grouping the column in SfDataGrid, i.e., it will show with both the Date and Time. However, you can skip showing the time in the GroupCaption by customizing the CaptionSummaryCellRenderers and update the GroupCaptionText in OnUpdateEditBinding method. Then, the GroupCaption will show only the Date while grouping the column in SfDataGrid. Please refer to the below code snippets.

<syncfusion:SfDataGrid x:Name="sfDataGrid"

                        AllowGrouping="True"

                        ShowGroupDropArea="True"

                        AllowEditing="True"

                        ItemsSource="{Binding Path=Orders}"

                        AutoGenerateColumns="False">

    <syncfusion:GridDateTimeColumn HeaderText="Order Date" GroupMode="Value"

                                    MappingName="Date"

                                    Pattern="ShortDate"/>

Renderer Customization

public class CustomCaptionSummaryCellRenderer : GridCaptionSummaryCellRenderer

    {

        /// <summary>

            /// Method to Update the CaptionSummaryCell.

            /// </summary>

        public override void OnUpdateEditBinding(DataColumnBase dataColumn, GridCaptionSummaryCell element, object dataContext)

        {

            if (element.DataContext is Group && this.DataGrid.View.GroupDescriptions.Count > 0)

            {

                object groupName ;

                string groupText = string.Empty;

                var groupRecord = element.DataContext as Group;

                //get the column which is grouped.

                var groupedColumn = this.GetGroupedColumn(groupRecord);

                var groupRecords = (groupRecord.Details as GroupRecordEntry).Records;

                var groupData = (groupRecords[0] as RecordEntry).Data;

                if (groupedColumn.MappingName == "Date")

                {

                    groupName = groupRecord.Key;

                    groupText = groupedColumn.HeaderText;

                }

                else

                {

                    groupName = groupRecord.Key;

                    groupText = groupedColumn.HeaderText;

                }

                if (this.DataGrid.CaptionSummaryRow == null)

                {

                    if (this.DataGrid.View.GroupDescriptions.Count < groupRecord.Level)

                        return;

                    //set the captionsummary text as customized.

                    element.Content = GetCustomizedCaptionText(groupText, groupName, groupRecord.ItemsCount);

                }

                else if (this.DataGrid.CaptionSummaryRow.ShowSummaryInRow)

                {

                    element.Content = SummaryCreator.GetSummaryDisplayTextForRow(groupRecord.SummaryDetails, this.DataGrid.View, groupedColumn.HeaderText);

                }

                else

                    element.Content = SummaryCreator.GetSummaryDisplayText(groupRecord.SummaryDetails, dataColumn.GridColumn.MappingName, this.DataGrid.View);

            }

        }

        /// <summary>

        /// Method to get the Column that is grouped.

        /// </summary>

        private GridColumn GetGroupedColumn(Group group)

        {

            var groupDesc = this.DataGrid.View.GroupDescriptions[group.Level - 1] as PropertyGroupDescription;

            foreach (var column in this.DataGrid.Columns)

            {

                if (column.MappingName == groupDesc.PropertyName)

                {

                    return column;

                }

            }

       

            return null;

        }

        /// <summary>

        /// Method to Customize the CaptionSummaryCell Text.

        /// </summary>

        private string GetCustomizedCaptionText(string columnName, object groupName, int itemsCount)

        {

            DateTime dateTime = (DateTime)groupName;

            var date = dateTime.ToShortDateString();

            return string.Format("{0}: {1} - {2}", columnName, date, itemsCount);

        }

 

    }


KB Link : https://support.syncfusion.com/kb/article/3433/how-to-customize-the-captionsummarycell-text-in-the-sfdatagrid

Let us know if you need any further assistance.


Attachment: SfDataGridDemo_37a52a19.zip


IK Ilia Korjoukov May 2, 2023 05:32 PM UTC

Dear  Sathiyathanam,

Thanks a lot, your solution worked for me with some minor adaptations! Note please that there can by typos in your code, these following block contains identical parts in "if ... else..."

if (groupedColumn.MappingName == "Date")

                {

                    groupName = groupRecord.Key;

                    groupText = groupedColumn.HeaderText;

                }

                else

                {

                    groupName = groupRecord.Key;

                    groupText = groupedColumn.HeaderText;

                }


Cheers,

Ilia




SJ Sathiyathanam Jeyakumar Syncfusion Team May 3, 2023 04:31 PM UTC

llia,

Thanks for the information. And we are glad that the provided response meets your requirement. Please let us know if you need further assistance. As always, we are happy to help you out.


Loader.
Live Chat Icon For mobile
Up arrow icon