0

Can you help me customise sorting in silverlight using PagedCollectionview mapped to an observablecollection.Below is the code that is working fine for the sort part but it does not refreshes the grid as the sorting from the first column is not cleared

Eg. if I sort it using "description" it works in both the direction(asc & desc).but after sorting the collection using "description" if I click on the "Type" Header it should clear the earlier sort & sort using the "Type" Column only.

   private void SortCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Reset)
            return;
        if (e.Action == NotifyCollectionChangedAction.Replace || e.Action == NotifyCollectionChangedAction.Add)
        {
            MyPVC.SortDescriptions.Clear();
            if (e.NewItems.Count > 0)
            {
                MyPVC.SortDescriptions.Clear();
                SortDescription sd = (SortDescription) e.NewItems[0];
                if (sd.PropertyName == "description")
                {
                    e.NewItems.Clear();
                    using (MyPVC.DeferRefresh())
                    {
                        ObservableCollection<MyClass> source = ((ObservableCollection<MyClass>)MyPVC.SourceCollection);
                        if (source == null)
                            return;
                        bool asc = (sd.Direction == ListSortDirection.Ascending);
                        var source1 = new List<MyClass>(source);
                        source1.Sort((a, b) =>
                        {
                            int left = 0;
                            int right = 0;
                            var ret = 0;
                            if (int.TryParse(a.description, out left) && int.TryParse(b.description, out right))
                            {
                                ret = (left < right) ? -1 : (left == right) ? 0 : 1;
                                if (!asc)
                                    ret = -ret;
                            }

                            return ret;
                        });

                        var newsource = new ObservableCollection<MyClass>(source1);
                        MyPVC = new PagedCollectionView(newsource);
                        ((INotifyCollectionChanged)MyPVC.SortDescriptions).CollectionChanged += SortCollectionChanged;
                        MyPVC.SortDescriptions.Clear();
                    }
                    MyClassDataGrid.ItemsSource = MyPVC;
                    MyPVC.Refresh();

                }
                if (sd.PropertyName == "Type")
                {
                    MyPVC.SortDescriptions.Clear();
                    //MyPVC = new PagedCollectionView(MyPVC);
                    e.NewItems.Clear();
                    using (MyPVC.DeferRefresh())
                    {
                        ObservableCollection<MyClass> source = ((ObservableCollection<MyClass>)MyPVC.SourceCollection);
                        if (source == null)
                            return;
                        bool asc = (sd.Direction == ListSortDirection.Ascending);
                        var source1 = new List<MyClass>(source);
                        source1.Sort((a, b) =>
                        {
                            int left = 0;
                            int right = 0;
                            var ret = 0;
                            if (int.TryParse(a.Type, out left) && int.TryParse(b.Type, out right))
                            {
                                ret = (left < right) ? -1 : (left == right) ? 0 : 1;
                                if (!asc)
                                    ret = -ret;
                            }

                            return ret;
                        });

                        var newsource = new ObservableCollection<MyClass>(source1);
                        MyPVC = new PagedCollectionView(newsource);
                        ((INotifyCollectionChanged)MyPVC.SortDescriptions).CollectionChanged += SortCollectionChanged;

                        MyPVC.SortDescriptions.Clear();
                    }
                }
            }
        }
    }
    private void MyClasstableView_ColumnHeaderClick(object sender, ColumnHeaderClickEventArgs e)
    {
        ((INotifyCollectionChanged) MyPVC.SortDescriptions).CollectionChanged += SortCollectionChanged;
    }
xoanon
  • 195
  • 1
  • 10

2 Answers2

1

It looks like you're doing way too much work here to implement a simple sort. The PagedCollectionView is designed to filter, sort, etc.. It will do the work for you. You have the basics right, but you don't have to do the source manipulation in order to get the sort to work.

Danexxtone
  • 773
  • 4
  • 11
  • Thanx for your reply Dane,but the collection has all the data in string format,I need to sort some data on the basis of date as well so i need to use conversion in it.hence i need these manipulations.I have made a few changes to the code as I need to update the grid I have used the begindataupdate()-EndDataUpdate() methods but it is giving me an error.which i believe is from the UI. – xoanon Oct 28 '13 at 06:09
0

Thanx for your reply Dane,but the collection has all the data in string format,I need to sort some data on the basis of date as well so i need to use conversion in it.hence i need these manipulations.I have made a few changes to the code as I need to update the grid I have used the begindataupdate()-EndDataUpdate() methods but it is giving me an error.which i believe is from the UI.

   private void SortCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Reset)
            return;
        if (e.Action == NotifyCollectionChangedAction.Replace || e.Action == NotifyCollectionChangedAction.Add)
        {
            MyPVC.SortDescriptions.Clear();
            if (e.NewItems.Count > 0)
            {
                MyPVC.SortDescriptions.Clear();
                SortDescription sd = (SortDescription) e.NewItems[0];
                if (sd.PropertyName == "description")
                {
                    e.NewItems.Clear();
                    using (MyPVC.DeferRefresh())
                    {
                        ObservableCollection<MyClass> source = ((ObservableCollection<MyClass>)MyPVC.SourceCollection);
                        if (source == null)
                            return;
                        bool asc = (sd.Direction == ListSortDirection.Ascending);
                        var source1 = new List<MyClass>(source);
                        source1.Sort((a, b) =>
                        {
                            int left = 0;
                            int right = 0;
                            var ret = 0;
                            if (int.TryParse(a.description, out left) && int.TryParse(b.description, out right))
                            {
                                ret = (left < right) ? -1 : (left == right) ? 0 : 1;
                                if (!asc)
                                    ret = -ret;
                            }

                            return ret;
                        });

                        var newsource = new ObservableCollection<MyClass>(source1);
                        MyPVC = new PagedCollectionView(newsource);
                        //((INotifyCollectionChanged)LeasePVC.SortDescriptions).CollectionChanged += SortCollectionChanged;

                        //LeasePVC.Refresh();
                        LeasePVC.SortDescriptions.Clear();

                        this.MyClassDataGrid.BeginDataUpdate();
                        this.MyClassDataGrid.ItemsSource = MyPVC;
                        this.MyClassDataGrid.EndDataUpdate();
                    }


                }
                if (sd.PropertyName == "Type")
                {
                    MyPVC.SortDescriptions.Clear();
                    //MyPVC = new PagedCollectionView(MyPVC);
                    e.NewItems.Clear();
                    using (MyPVC.DeferRefresh())
                    {
                        ObservableCollection<MyClass> source = ((ObservableCollection<MyClass>)MyPVC.SourceCollection);
                        if (source == null)
                            return;
                        bool asc = (sd.Direction == ListSortDirection.Ascending);
                        var source1 = new List<MyClass>(source);
                        source1.Sort((a, b) =>
                        {
                            int left = 0;
                            int right = 0;
                            var ret = 0;
                            if (int.TryParse(a.Type, out left) && int.TryParse(b.Type, out right))
                            {
                                ret = (left < right) ? -1 : (left == right) ? 0 : 1;
                                if (!asc)
                                    ret = -ret;
                            }

                            return ret;
                        });

                        var newsource = new ObservableCollection<MyClass>(source1);
                        MyPVC = new PagedCollectionView(newsource);
                        //((INotifyCollectionChanged)LeasePVC.SortDescriptions).CollectionChanged += SortCollectionChanged;

                        //LeasePVC.Refresh();
                        LeasePVC.SortDescriptions.Clear();

                        this.MyClassDataGrid.BeginDataUpdate();
                        this.MyClassDataGrid.ItemsSource = MyPVC;
                        this.MyClassDataGrid.EndDataUpdate();
                    }
                }
            }
        }
    }
    private void MyClasstableView_ColumnHeaderClick(object sender, ColumnHeaderClickEventArgs e)
    {
        ((INotifyCollectionChanged) MyPVC.SortDescriptions).CollectionChanged += SortCollectionChanged;
    }
xoanon
  • 195
  • 1
  • 10
  • What is the error you're getting? And could you show the class of the data as well? It would greatly help. – Danexxtone Oct 28 '13 at 15:44
  • thanx Dane for you time I have got a workaround fro this problem.I am sorting the collection on the basis of other field – xoanon Oct 29 '13 at 12:33