Custom sorting collapses all groups

3 posts, 0 answers
  1. Martin
    Martin avatar
    2 posts
    Member since:
    Dec 2014

    Posted 26 Aug 2015 Link to this post

     Normally when you sort a column wether the groups are open or closed is remembered.

     But when adding Custom sorting to RadGridView and all the groups are collapsed when sorting, but if you add AutoExpandGroups all groups are expanded.

     I figure the reason is that the items are re-added to the collection.

     

    I would really like to have the default behaviour, where the open groups are open and the closed are closed, event after sorting.

    Is there a way to do this??

     

    Regards

    Martin

  2. Martin
    Martin avatar
    2 posts
    Member since:
    Dec 2014

    Posted 26 Aug 2015 in reply to Martin Link to this post

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DataContext = new MyClass()
                {
                    MyCollection = new List<RowClass>()
                    {
                       new RowClass(){Type = "Large"},
                       new RowClass(){Type = "Large"},
                       new RowClass(){Type = "Small"},
                       new RowClass(){Type = "Small"},
                    }
                };
     
                this.radGridView.Sorting += this.CustomSortingGrid_Sorting; 
            }
            private void CustomSortingGrid_Sorting(object sender, GridViewSortingEventArgs e)
            {
                //Gets the value of the ItemsSource property as IEnumerable.
                IEnumerable<RowClass> classes = e.DataControl.ItemsSource as IEnumerable<RowClass>;
                //Checks if the value of the collection is null.
                if (classes == null)
                {
                    e.Cancel = true;
                    return;
                }
                //If the sorting state is none, sort the items ascending.
                if (e.OldSortingState == SortingState.None)
                {
                    e.NewSortingState = SortingState.Ascending;
                    classes = classes.OrderBy(employee => employee.GetType()
                                                                       .GetProperty((e.Column as GridViewDataColumn).GetDataMemberName())
                                                                       .GetValue(employee, null));
                }
                //If the sorting state is none, sort the items descending.
                else if (e.OldSortingState == SortingState.Ascending)
                {
                    e.NewSortingState = SortingState.Descending;
                    classes = classes.OrderByDescending(employee => employee.GetType()
                                                                        .GetProperty((e.Column as GridViewDataColumn).GetDataMemberName())
                                                                        .GetValue(employee, null));
                }
                //If the sorting state is descending, apply default sorting to the items.
                else
                {
                    e.NewSortingState = SortingState.None;
                    classes = classes.OrderBy(employee => employee.Type);
                }
                //Set the sorted collection as source of the RadGridView
                e.DataControl.ItemsSource = classes.ToList();
                e.Cancel = true;
            }
        }
     
        public class RowClass
        {
            public string Type { get; set; }
        }
     
        public class MyClass
        {
            public IEnumerable<RowClass> MyCollection { get; set; }
        }

     

    <Window x:Class="WpfApplication9.MainWindow"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <telerik:RadGridView Name="radGridView" ItemsSource="{Binding MyCollection}" AutoGenerateColumns="False"
                                 AutoExpandGroups="False">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Type}">
                         
                    </telerik:GridViewDataColumn>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </Grid>
    </Window>

  3. UI for WPF is Visual Studio 2017 Ready
  4. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 26 Aug 2015 Link to this post

    Hello Martin,

    First of all, have you considered the other two alternatives for custom sorting - implementing IComaparable, or using a generic SortDescriptor? In case that these options do not work for your case and you still want to stick to the approach with handling the sorting event, you can try not to reset the ItemsSource, but to create batch update. However, you will need a collection that supports batch updates, without calling INotifyCollectionChanged on every added item (otherwise this may prove to be extremely slow). I prepared a sample project that demonstrates this approach.

    Regards,
    Ivan Ivanov
    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