Apply custom sorting in a grouping column binding to a string property programatically

4 posts, 0 answers
  1. FMorales
    FMorales avatar
    26 posts
    Member since:
    Aug 2014

    Posted 17 Feb 2015 Link to this post

    Hi, I am trying to apply custom sorting to a grouping column designed programatically that is binding to the grid as string.

    The problem is that I cannot get event fired because for example I do not allow user to sort the columns, It is made programatically  and it is fixed by design.

    How can I apply a custom sorting method to a grid designed programatically for a grouping column and that it is fixed ??


    Column         Column
    ======         ======
    4                    1
    2;33               2
    1;35               4
    11                  5
    2                    11
    1                    13
    13                  1;35
    5                    2;33

    I have been trying to apply this example but I can not get it works, for example events do not fire.

    Any help would be appreciated.

    // radGrid
    this.radGrid.MasterTemplate.AllowSearchRow = true;
    this.radGrid.MasterTemplate.AutoExpandGroups = true;
    this.radGrid.MasterTemplate.EnableFiltering = true;
    this.radGrid.MasterTemplate.MultiSelect = true;
    this.radGrid.MasterTemplate.ShowFilteringRow = false;
    this.radGrid.MasterTemplate.ShowHeaderCellButtons = true;
    this.radGrid.MasterTemplate.HorizontalScrollState = Telerik.WinControls.UI.ScrollState.AlwaysShow;
    this.radGrid.Name = "radGrid";
    this.radGrid.ReadOnly = true;
    this.radGrid.ShowHeaderCellButtons = true;
    this.radGrid.Size = new System.Drawing.Size(1028, 262);
    this.radGrid.TabIndex = 5;
    this.radGrid.Text = "Test Results Browser Grid";
    this.radGrid.SelectionChanged += new System.EventHandler(this.radGrid_SelectionChanged);
    this.radGrid.CellClick += new Telerik.WinControls.UI.GridViewCellEventHandler(this.radGrid_CellClick);
    this.radGrid.ContextMenuOpening += new Telerik.WinControls.UI.ContextMenuOpeningEventHandler(this.radGrid_ContextMenuOpening);
    this.radGrid.FilterExpressionChanged += new Telerik.WinControls.UI.GridViewFilterExpressionChangedEventHandler(this.radGrid_FilterExpressionChanged);
    this.radGrid.FilterChanged += new Telerik.WinControls.UI.GridViewCollectionChangedEventHandler(this.radGrid_FilterChanged);
    this.radGrid.Click += new System.EventHandler(this.radGrid_Click);
    private void ConfigureRadGridGrouping()
      GroupDescriptor groupByBall = new GroupDescriptor();
      groupByBall.GroupNames.Add("Balls", ListSortDirection.Ascending);
      this.radGrid.EnableCustomGrouping = false;
    private void AsignTitlesToRadGridBrowser()
       /// Telerik
       foreach (var column in this.radGrid.Columns)
        column.HeaderText = string.Empty;
        column.IsVisible = false;
        column.VisibleInColumnChooser = false;
       this.radGrid.Columns["Reference"].HeaderText = "Reference";
       this.radGrid.Columns["Reference"].IsVisible = true;
       this.radGrid.Columns["Reference"].VisibleInColumnChooser = true;
       this.radGrid.Columns["Reference"].AllowGroup = true;
       this.radGrid.Columns["Title"].HeaderText = "Title";
       this.radGrid.Columns["Title"].IsVisible = true;
       this.radGrid.Columns["Title"].VisibleInColumnChooser = true;
       this.radGrid.Columns["Title"].AllowGroup = true;
       this.radGrid.Columns["Balls"].HeaderText = "Balls";
       this.radGrid.Columns["Balls"].IsVisible = true;
       this.radGrid.Columns["Balls"].VisibleInColumnChooser = true;
       this.radGrid.Columns["Balls"].AllowGroup = true;
  2. Dess
    Dess avatar
    1561 posts

    Posted 19 Feb 2015 Link to this post

    Hello Francisco,

    Thank you for writing.

    Custom sorting is a flexible mechanism for sorting RadGridView data rows using custom logic. When it comes to sorting groups it is suitable to use a GroupComparer. Thus, you can parse each group key to integer and compare the numeric representation instead of the string. Here is an example:
    public Form1()
        DataTable dt = new DataTable();
        dt.Columns.Add("A", typeof(string));
        dt.Columns.Add("B", typeof(string));
        dt.Columns.Add("C", typeof(string));
        dt.Rows.Add("DataA.1", "1", "DataC.1");
        dt.Rows.Add("DataA.2", "5", "DataC.2");
        dt.Rows.Add("DataA.3", "10", "DataC.3");
        dt.Rows.Add("DataA.4", "11", "DataC.4");
        dt.Rows.Add("DataA.5", "20", "DataC.5");
        dt.Rows.Add("DataA.6", "2", "DataC.6");
        dt.Rows.Add("DataA.7", "3", "DataC.7");
        this.radGridView1.DataSource = dt;
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.MasterTemplate.GroupComparer = new GroupComparer();      
        GroupDescriptor descriptor = new GroupDescriptor();
        descriptor.GroupNames.Add("B", ListSortDirection.Ascending);
    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))
                return parsedX.CompareTo(parsedY);
            return x.Key.ToString().CompareTo(y.Key.ToString());

    I hope this information helps. Should you have further questions, I would be glad to help.

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

  3. DevCraft R3 2016 release webinar banner
  4. Mike
    Mike avatar
    6 posts
    Member since:
    Sep 2015

    Posted 18 Sep 2015 in reply to Dess Link to this post

    This is an excellent example, and it ALMOST solves the issue I'm having.

    I need to apply the custom group sorting to only a specific field. The example in this post attempts to apply it to all fields (A, B and C).

    Actually, my specific situation needs to have a custom group sorting algorithm that is different for each field (A, B and C).

    How do I accomplish this?


  5. Dess
    Dess avatar
    1561 posts

    Posted 21 Sep 2015 Link to this post

    Hello Michael,

    Thank you for writing.
    The GroupComparer is set globally to the MasterTemplate. Its Compare method will be called every time when two groups need to be compared. You have access to the PropertyName for which you perform grouping. Hence, you can construct your sort logic. Please refer to the following forum post which demonstrates how to compare groups considering the property name:

    I hope this information helps. Should you have further questions I would be glad to help.
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top