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

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

3 Answers 254 Views
GridView
This is a migrated thread and some comments may be shown as answers.
FMorales
Top achievements
Rank 1
FMorales asked on 17 Feb 2015, 08:23 AM
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 ??

example:

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.
http://www.telerik.com/help/winforms/gridview-sorting-custom-sorting.html

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;
  this.radGrid.GroupDescriptors.BeginUpdate();
  this.radGrid.GroupDescriptors.Add(groupByBall);
  this.radGrid.GroupDescriptors.EndUpdate();
}
 
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;
}  

3 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 19 Feb 2015, 08:39 AM
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()
{
    InitializeComponent();
 
    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);
    this.radGridView1.GroupDescriptors.Add(descriptor);
}
 
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.
 
Regards,
Dess
Telerik
 

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.

 
0
Mike
Top achievements
Rank 1
answered on 18 Sep 2015, 12:35 PM

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?

Michael

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Sep 2015, 11:33 AM
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:  http://www.telerik.com/forums/grouping-column-with-listsortdirection-none

I hope this information helps. Should you have further questions I would be glad to help.
 
Regards,
Dess
Telerik
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
Tags
GridView
Asked by
FMorales
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Mike
Top achievements
Rank 1
Share this question
or