This is a migrated thread and some comments may be shown as answers.

GroupComparer and EnableCustomSorting does not work together

1 Answer 97 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 06 Jan 2021, 09:06 AM

Hello,

I have a GridView with GroupComparer and it works fine, but if I add EnableCustomSorting true, the GroupComparer stops working, it's not called by GridView anymore. Could you please advise how to make it work together.

 

public Form1()
{
  InitializeComponent();
 
  DataTable dt = new DataTable();
  dt.Columns.Add( "A", typeof( string ) );
 
  this.radGridView1.Columns.Add( "A" );
  this.radGridView1.Columns.Add( "B" );
 
  this.radGridView1.Rows.Add( "1", "1");
  this.radGridView1.Rows.Add( "2", "2" );
  this.radGridView1.Rows.Add( "10", "10" );
 
  this.radGridView1.MasterTemplate.GroupComparer = new GroupComparer();
 
  this.radGridView1.CustomSorting += RadGridView1_CustomSorting;
  this.radGridView1.EnableCustomSorting = true; //if true, GroupComparer stop working
}
 
private void RadGridView1_CustomSorting( object sender, GridViewCustomSortingEventArgs e )
{
  //logic to sort columns.
}
 
public class GroupComparer : IComparer<Group<GridViewRowInfo>>
{
  public int Compare( Group<GridViewRowInfo> x, Group<GridViewRowInfo> y )
  {
    int parsedX;
    int parsedY;
    if ( int.TryParse( ( (object[])x.Key ).First().ToString(), out parsedX ) &&
        int.TryParse( ( (object[])y.Key ).First().ToString(), out parsedY ) )
    {
      int result = parsedX.CompareTo( parsedY );
      DataGroup xGroup = x as DataGroup;
      if ( xGroup != null && ( (DataGroup)x ).GroupDescriptor.GroupNames.First().Direction == ListSortDirection.Descending )
      {
        result *= -1;
      }
      return result;
    }
    return ( (object[])x.Key )[0].ToString().CompareTo( ( (object[])y.Key )[0].ToString() );
  }
}

1 Answer, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 07 Jan 2021, 07:08 AM
Hello, Thomas,   

When you set the EnableCustomSorting property to true, the MasterTemplate.GroupComparer gets reset. That is why the custom comparer is not used. If you want to enable the custom sorting functionality and still keep using the custom group comparer, feel free to set the MasterTemplate.GroupComparer property after setting the EnableCustomSorting property to true:  
            this.radGridView1.CustomSorting += RadGridView1_CustomSorting;
            this.radGridView1.EnableCustomSorting = true;  

            this.radGridView1.MasterTemplate.GroupComparer = new GroupComparer();
I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
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
Thomas
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or