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

RadGridView: Changing Sort in SortChanging re-fires SortChanging

2 Answers 139 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Trey
Top achievements
Rank 2
Trey asked on 28 Aug 2015, 05:17 PM

When the column header of a RadGridView is clicked, it fires the SortChanging event.

 In this event, I am executing the following code:

 

Telerik.WinControls.UI.GridViewTemplate gwt = (Telerik.WinControls.UI.GridViewTemplate)sender;
 
Telerik.WinControls.Data.SortDescriptor sort = (Telerik.WinControls.Data.SortDescriptor)e.NewItems[0];
 
Telerik.WinControls.Data.SortDescriptor sort1 = new Telerik.WinControls.Data.SortDescriptor()
{
    PropertyName = "sortorder",irection = System.ComponentModel.ListSortDirection.Ascending
};
 
gwt.SortDescriptors.Clear();
 
gwt.SortDescriptors.Add(sort1);
gwt.SortDescriptors.Add(sort);
 
‚Äč

This re-fires the SortChanging event, resulting in an infinite loop.

 How can I suppress the refiring of the SortChanging event?

2 Answers, 1 is accepted

Sort by
0
Trey
Top achievements
Rank 2
answered on 28 Aug 2015, 08:16 PM

I found a solution.  I disabled column sorting, and used the CellClick event as follows:

 

private void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
{
    if (e.Row is GridViewTableHeaderRowInfo)
    {
         Telerik.WinControls.UI.GridHeaderCellElement ghce= (Telerik.WinControls.UI.GridHeaderCellElement)sender;
 
         Telerik.WinControls.UI.GridViewTemplate gwt = (Telerik.WinControls.UI.GridViewTemplate)ghce.ViewTemplate;
 
         var ChildSort = new Telerik.WinControls.Data.SortDescriptor()
         {
             PropertyName = e.Column.Name ,
             Direction = System.ComponentModel.ListSortDirection.Ascending
         };
 
         var ChildSort2 = new Telerik.WinControls.Data.SortDescriptor()
         {
             PropertyName = "sortorder",
             Direction = System.ComponentModel.ListSortDirection.Ascending
         };
 
          gwt.SortDescriptors.Clear();
          gwt.SortDescriptors.Add(ChildSort2);
          gwt.SortDescriptors.Add(ChildSort);
     }
}

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 31 Aug 2015, 10:48 AM
Hello Trey,

Thank you for writing.

I am glad that you have found a suitable solution for your specific case. However, note that when you programmatically add SortDescriptors, the SortChanging and SortChanged events are also fired. The SortChanging event is raised before the data is sorted. In the event handler the RadGridView.SortDescriptors collection is not updated yet as this is a cancelable event. If you need to clear the SortDescriptors collection, it is more suitable to use the SortChanged event when the sorting operation has already finished. Please refer to the following code snippet demonstrating how to manipulate the SortDescriptors collection avoiding an infinite loop and add programmatically a new SortDescriptor:
private void radGridView1_SortChanged(object sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
{
    if (e.NewItems != null)
    {
        this.radGridView1.SortChanged -= radGridView1_SortChanged;
 
        //add SortDescriptors according to your requirement
        Telerik.WinControls.UI.GridViewTemplate gwt = (Telerik.WinControls.UI.GridViewTemplate)sender;
        Telerik.WinControls.Data.SortDescriptor sort = (Telerik.WinControls.Data.SortDescriptor)e.NewItems[0];
 
        Telerik.WinControls.Data.SortDescriptor sort1 = new Telerik.WinControls.Data.SortDescriptor()
        {
            PropertyName = "ContactName", Direction = System.ComponentModel.ListSortDirection.Ascending
        };
 
        gwt.SortDescriptors.Clear();
        gwt.SortDescriptors.Add(sort1);
        gwt.SortDescriptors.Add(sort);
 
        this.radGridView1.SortChanged += radGridView1_SortChanged;
    }
}

Feel free to modify the code snippet on a way to achieve the desired behavior.

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
General Discussions
Asked by
Trey
Top achievements
Rank 2
Answers by
Trey
Top achievements
Rank 2
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or