Item Order number sorting in Grouped SfDataGrid

Hi Team,
In my application, I'm grouped by Location, DeliveryTo, and Tracking Number. after grouping Item order values are shuffled. But my requirement is it has to display 1,2,3,4...etc. Is there any auto-generated serial number for Syncfusion lib? Please provide the solution for this.

report1.png  


3 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team September 2, 2022 05:18 PM UTC

Hi Maharana Kirankumar,

Whenever we group the column the sorting operation will perform only for the group, not for grouped records. The reported scenario is not an issue. This is the expected behavior in SfDataGrid.


However, your requirement to sort the order values after grouping in SfDataGrid can be achieved by applying custom sorting for the first grouped column. Please refer to the below code snippet,


XAML Code Snippet:

<Page.Resources>      

        <local:CustomComparer x:Key="comparer" />

    </Page.Resources>

    <Grid>

        <syncfusion:SfDataGrid Margin="30" 

                               x:Name="dataGrid"

                               AutoGenerateColumns="True"  

                               AutoExpandGroups="True"                            

                               ShowGroupDropArea="True"

                               ItemsSource="{Binding OrderList}"  >

            <!--by default here set the SortDirection as Ascending for order values-->

            <syncfusion:SfDataGrid.SortColumnDescriptions>

                <syncfusion:SortColumnDescription ColumnName="Order"  SortDirection="Ascending" />

            </syncfusion:SfDataGrid.SortColumnDescriptions>

 

            <!--custom sorting applied for first grouped column in SfDataGrid (ex:Location column is first grouped)-->

            <syncfusion:SfDataGrid.SortComparers>

                <Linq:SortComparer Comparer="{StaticResource comparer}" PropertyName="Location" />

            </syncfusion:SfDataGrid.SortComparers>

        

            <syncfusion:SfDataGrid.GroupColumnDescriptions>

                <syncfusion:GroupColumnDescription ColumnName="Location"  />

                <syncfusion:GroupColumnDescription ColumnName="DeliveryTo" />

                <syncfusion:GroupColumnDescription ColumnName="TrackingNumber" />

            </syncfusion:SfDataGrid.GroupColumnDescriptions>        

        </syncfusion:SfDataGrid>

    </Grid>


C# Code snippet related to CustomComparer:

public class CustomComparer : IComparer<object>, ISortDirection

    {

        private 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 ListSortDirection SortDirection

        {

            get { return _SortDirection; }

            set { _SortDirection = value; }

        }

 

        public int Compare(object x, object y)

        {

            int nameX;

            int nameY;

 

            //While data object passed to comparer

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

            {

 

                nameX = ((OrderInfo)x).Order;

                nameY = ((OrderInfo)y).Order;

            }

            //While sorting groups

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

            {

                //here customize based on your scenario

                var getXBottomGroup = CheckGroupX(x as Group);

                var getYBottomGroup = CheckGroupY(y as Group);

                int xProductId = -1;

                int yProductId = -1;

                if (getXBottomGroup.Records != null)

                {

                    foreach (var record in getXBottomGroup.Records)

                    {

                        //here get the which column should sorted in SfDataGrid

                        xProductId = (record.Data as OrderInfo).Order;

                    }

                }

 

                if (getYBottomGroup.Records != null)

                {

                    foreach (var record in getYBottomGroup.Records)

                    {

                        //here get the which column should sorted in SfDataGrid

                        yProductId = (record.Data as OrderInfo).Order;

                    }

                }

 

                //Calculating the group key length

                nameX = xProductId;

                nameY = yProductId;

            }

            else

            {

                nameX = (int)x;

                nameY = (int)y;

            }

 

            //returns the comparison result based in SortDirection.

            if (nameX.CompareTo(nameY) > 0)

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

 

            else if (nameX.CompareTo(nameY) == -1)

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

 

            else

                return 0;

        }

 

        private Group CheckGroupY(Group group)

        {

            //Check whether the group is null or not

            if (group != null && group.Groups != null)

            {

                //if more than one group is achieved ,you need to check those levels and get the records

                foreach (var gr in group?.Groups)

                {

                    if (gr.IsBottomLevel)

                        return gr;

                    else

                        //Called recursively , to traverse it inner level of group.

                        return CheckGroupY(gr);

                }

 

 

            }

            return group;

 

        }

 

        private Group CheckGroupX(Group group)

        {

            //Check whether the group is null or not

            if (group != null && group.Groups != null)

            {

                //if more than one group is achieved ,you need to check those levels and get the records

                foreach (var gr in group?.Groups)

                {

                    if (gr.IsBottomLevel)

                        return gr;

                    else

                        //Called recursively , to traverse it inner level of group.

                        return CheckGroupX(gr);

                }

 

            }

            return group;

 

        }

    }


UG Link: https://help.syncfusion.com/uwp/datagrid/sorting#custom-sorting

https://help.syncfusion.com/uwp/datagrid/grouping#custom-grouping

Please find the sample in the attachment and let us know if you have any concerns in this.


Regards,

Vijayarasan S

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: Sample_a0728b19.zip


MK Maharana Kirankumar September 2, 2022 06:04 PM UTC

Hi Vijayarasan 

Finally I reached what I want. Thank you for your replay



Marked as answer

VD Vasanthkumar Durairaj Syncfusion Team September 5, 2022 02:58 PM UTC

Hi Maharana Kirankumar,

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you.


Regards,
Vasanthkumar D


Loader.
Up arrow icon