RadGridView: Changing Sort in SortChanging re-fires SortChanging

3 posts, 0 answers
  1. Trey
    Trey avatar
    35 posts
    Member since:
    Dec 2006

    Posted 28 Aug 2015 Link to this post

    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. Trey
    Trey avatar
    35 posts
    Member since:
    Dec 2006

    Posted 28 Aug 2015 in reply to Trey Link to this post

    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);
         }
    }

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 31 Aug 2015 Link to this post

    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
Back to Top