Grouping objects in RadGridView

2 posts, 0 answers
  1. DTA
    DTA avatar
    6 posts
    Member since:
    Aug 2012

    Posted 28 May 2014 Link to this post

    We are having grouping objects in the RadGridView.
    For example ,one of the properties of our objects is named: Changed.
    When the application is loaded, the RadGridView and the GridViewScrollbar behave well.
    after grouping if the value of the property(Changed) changes in a specific object,then it should move to the appropriate group.
    Unfortuntely,this does not happen.
    To solve this bug ,we tried to manually sort or regroup.
    This bug solved the currnt,but created another bug.
    The GridViewScrollbar of the grid moves up suddenly and does not stay in its original position.
    The vertical offset=0;
    In addition , when we return to the initial state (cancel the grouping), the GridViewScrollbar still changes it's position.
    The objects in the item source update all the time.
    In addition ,we raise a property changed event on the collection bound to the item source .

    How is this accomplished?
    Thanks in advance for your help.
    public class MyDataContext
        {
            readonly string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            Random rnd ;

    ObservableCollection<StockData> _source;
    ObservableCollection<StockData> Source
            {
                get
                {
                   if (_source == null)
                    {
                           rnd=new Random();

                           _source = new ObservableCollection<StockData>(from i in Enumerable.Range(0, 20) select CreateStockData());

                           DispatcherTimer timer = new DispatcherTimer() 
                                       { 
                                             Interval = TimeSpan.FromSeconds(10),
                                              timer.Tick += new EventHandler(timer_Tick),
                                             timer.Start()
                                         };

                         return this.source;
                }
            }

            void timer_Tick(object sender,EventArgs e)
            { 
                    switch(source [0].Change)
                    {
                              case 0 :
                                    source [0] .Change = 1 ;
                                     break;
                               case 1 :
                                     source [0] .Change = 0 ;
                                     break;
                     }
                 SortCollection() ; 
            }

            private StockData CreateStockData()
            {
                StockData item = new StockData();
                item.Name = String.Format("{0}{1}{2}{3}", this.letters[this.rnd.Next(0, this.letters.Count())], this.letters[this.rnd.Next(0, this.letters.Count())],
                                                                  this.letters[this.rnd.Next(0, this.letters.Count())], this.letters[this.rnd.Next(0, this.letters.Count())]);
                item.LastUpdate = DateTime.Now;
                item.Change = rnd.Next(0,2);
                return item;
            }

            QueryableCollectionView _data;
            public QueryableCollectionView Data
            {
                get
                {
                     if (this.data == null)
                    {
                          _data = new QueryableCollectionView(Source);
                          _data.SortDescriptors.Add(new SortDescriptor()
                          {
                                   Member = "Name",
                                   SortDirection = System.ComponentModel.ListSortDirection.Descending
                          });

                        _data.SortDescriptors.Add(new SortDescriptor()
                       { 
                                  Member = "Name",
                                 SorttDirection = System.ComponentModel.ListSortDirection.Ascending
                       });
                    }

                   return _data;
                }
            }

            private void SortCollection()
            {
                       _data.SortDescriptors.Add(new SortDescriptor()
                             {
                                   Member = "Name",
                                   SortDirection = System.ComponentModel.ListSortDirection.Descending
                             });

                        _data.SortDescriptors.Add(new SortDescriptor()
                              { 
                                  Member = "Name",
                                 SorttDirection = System.ComponentModel.ListSortDirection.Ascending
                              });
            }  

        Public class StockData
        {
            public string Name{get;set;}
            public int Change{get;set;}
            public DateTime LastUpdate{get;set;}
         }
    }







  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 02 Jun 2014 Link to this post

    Hello,

    If you update the value from the UI, then the item should be regrouped. Otherwise, if you update the value of the property (Changed) from code, then you should ensure a CollectionChanged notification is raised so that the UI is notified for the change. When you manually sort, group or invoke a Rebind(), a CollectionChanged notification with Action Reset is raised. This is the reason why the value is updated. You can check this article for more details. It is for updating the aggregates, however, the idea regarding updating the item with the changed property would be the same.

    You say that the solution you found creates another bug, resetting the GridViewScrollBar. The reason for this that every data operation, plus invoking a Rebind() will raise  a CollectionChanged event with Action System.Collections.Specialized.NotifyCollectionChangedAction.Reset.  Please note that just sorting any column from your RadGridView will invoke a Rebind internally.

    I can recommend you using the approach from the Update item outside RadGridView help article. That way such a notification will be raised just for the item being actually changed. As a result, the item will be updated without resetting the GridViewScrollBar.

    I hope this helps.

    Regards,
    Didie
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top