Gridview custom groupcomparer instantiation

3 Answers 116 Views
GridView
Daniel
Top achievements
Rank 2
Iron
Iron
Daniel asked on 25 Jan 2023, 02:48 PM

Hi Telerik support team,

I’m strugling with the Winforms GridView GroupComparer and need some help with it. I’ve seen the topic below:

https://docs.telerik.com/devtools/winforms/controls/gridview/grouping/sorting-group-rows

In my case I’ve a generic comparer in which I want to evaluate which column needs some sorting changes. These are my unitColumns and I have a reference to its list at the moment I create the comparer.

this.radGridView1.MasterTemplate.GroupComparer = new CustomGroupComparer(list);

 

The problem I walk into is that Telerik creates new CustomGroupComparers by itself (I think for every group?). It also demands an empty constructor. This way I have no option to get to the list that I need.

Is there a workaround for this problem?? For this moment I defined a hardcoded List inside the customComparer but that’s not the way to program it.

It would be nice if the custom comparer was part of the GroupDescriptor.

Regards,

Daniel

3 Answers, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 30 Jan 2023, 11:08 AM

Hello Daniel,

Thank you for writing us.

I have read your scenario but I think I will need to have a look at when the custom GroupComparer is set. Could it be possible to modify the attached project to mimic your scenario so that I can debug it and think of a suitable solution? 

I am looking forward to your reply.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Daniel
Top achievements
Rank 2
Iron
Iron
answered on 08 Feb 2023, 01:48 PM

Hi Dinko,

I was not able to edit your project due to reference issues. Here is the content of my simplified groupcomparer. My intention and the issue I run into should be clear..

    public class CustomGroupComparer : IComparer<Group<GridViewRowInfo>>
    {
        private List<string> _unitColumns = new List<string>
        {
            {"Length"},
            {"Sawlength"},
        };
        //public Func<string, bool> UnitColumnsContainsKey;

        public int Compare(Group<GridViewRowInfo> x, Group<GridViewRowInfo> y)
        {
            if (x.GetType().Name != y.GetType().Name)
            {
                return x is DataItemGroup<GridViewRowInfo> ? 1 : -1;
            }

            var dataGroup = (DataGroup)x;
            var columnName = dataGroup.GroupDescriptor.GroupNames.FirstOrDefault().PropertyName;
            if (columnName != null && _unitColumns.Contains(columnName))
            {
                // grouped column exists
                // do some custom work and custom compareTo..
                // for ilustation now default..
                return x.Key.ToString().CompareTo(y.Key.ToString());
            }
            else
            {
                // not part of the list.. default compare
                return x.Key.ToString().CompareTo(y.Key.ToString());
            }
        }
    }

 

So the issue is that I don't want the list _unitColumns in the comparer. Because I can't use a own constructor for the comparer, I can't pass through a reference to my list (which exists elsewhere).

Telerik demands a empty constructor which is used at instantiating the customComparer.

The question is: how can I get a reference to an object inside the groupComparer?? If it is possible to interrupt at the place where Telerik instantiates the customComparer, it could be solved easy by setting a property or even call a different constructor in which my list reference can be passed.

Regards,

Daniel

0
Dinko | Tech Support Engineer
Telerik team
answered on 09 Feb 2023, 02:01 PM

Hi Daniel,

Thank you for the additional code.

I see what you are trying to achieve here. Indeed, the group-comparer is instantiated with reflection, by calling the parameterless constructor. The engine in RadGridView does not know of the particular implementation of your custom group-comparеr so it cannot pass a parameter to match the signature of your constructor.

What I can suggest here is to create a static property inside your custom group compared. This way you could have access to your custom collection.

public class CustomGroupComparer : IComparer<Group<GridViewRowInfo>>
{
    private static List<string> _unitColumns;
    public static List<string> UnitColumns
    {
        get { return _unitColumns; }
        set { _unitColumns = value; }
    }

    public int Compare(Group<GridViewRowInfo> x, Group<GridViewRowInfo> y)
    {
        if (x.GetType().Name != y.GetType().Name)
        {
            return x is DataItemGroup<GridViewRowInfo> ? 1 : -1;
        }

        var dataGroup = (DataGroup)x;
        var columnName = dataGroup.GroupDescriptor.GroupNames.FirstOrDefault().PropertyName;
        if (columnName != null && UnitColumns.Contains(columnName))
        {
            // grouped column exists
            // do some custom work and custom compareTo..
            // for ilustation now default..
            return x.Key.ToString().CompareTo(y.Key.ToString());
        }
        else
        {
            // not part of the list.. default compare
            return x.Key.ToString().CompareTo(y.Key.ToString());
        }
    }
}

CustomGroupComparer.UnitColumns =  new List<string>
                                {
                                    {"Length"},
                                    {"Sawlength"},
                                }; 
this.radGridView1.MasterTemplate.GroupComparer = new CustomGroupComparer();

I hope that this approach will work for you.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
GridView
Asked by
Daniel
Top achievements
Rank 2
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Daniel
Top achievements
Rank 2
Iron
Iron
Share this question
or