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

Custom sorting collapses all groups

2 Answers 115 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 26 Aug 2015, 11:49 AM

 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 Answers, 1 is accepted

Sort by
0
Martin
Top achievements
Rank 1
answered on 26 Aug 2015, 11:51 AM
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>

0
Ivan Ivanov
Telerik team
answered on 26 Aug 2015, 03:18 PM
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
Tags
GridView
Asked by
Martin
Top achievements
Rank 1
Answers by
Martin
Top achievements
Rank 1
Ivan Ivanov
Telerik team
Share this question
or