Grid not updating when data changed

2 posts, 0 answers
  1. John
    John avatar
    16 posts
    Member since:
    Feb 2012

    Posted 24 Sep Link to this post

    The grid does not seem to be subscribing to PropertyChanged on INotifyPropertyChanged. All the bound data objects provide INotifyPropertyChanged, but when they go to fire this event the event handler is null; meaning no one is listening. The grid keeps showing the old data until I select the row.

     

    Here is code to reproduce it:

    public partial class RadForm1 : RadForm
    {
        private Timer _timer;
        private ObservableCollection<GridData> _data;
     
        public static bool GridBound = false;
     
        public RadForm1()
        {
            InitializeComponent();
        }
     
        protected override void OnLoad( EventArgs e )
        {
            base.OnLoad( e );
     
            var gv = new RadGridView();
            gv.Dock = DockStyle.Fill;
            Controls.Add( gv );
     
            _data = new ObservableCollection<GridData>()
            {
                new GridData { First = "A", Second = "B", Third = "C" },
                new GridData { First = "AA", Second = "BB", Third = "CC" },
                new GridData { First = "AAA", Second = "BBB", Third = "CCC" }
            };
            gv.DataSource = _data;
            GridBound = true;
     
            _timer = new Timer();
            _timer.Tick += _timer_Tick;
            _timer.Interval = 1000;
            _timer.Start();
        }
     
        private void _timer_Tick( object sender, EventArgs e )
        {
            _data[0].First = "D";
            _data[1].Second = "EE";
            _data[2].Third = "FFF";
     
            _timer.Stop();
        }
     
        private class GridData : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged( string name )
            {
                if( PropertyChanged == null && RadForm1.GridBound )
                {
                    Debug.WriteLine( $"PropertyChanged is null, grid has not subscribed! Name = '{name}'" );
                }
     
                PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( name ) );
            }
     
            private string _first;
            private string _second;
            private string _third;
     
            public string First { get => _first; set { _first = value; OnPropertyChanged( "First" ); } }
            public string Second { get => _second; set { _second = value; OnPropertyChanged( "Second" ); } }
            public string Third { get => _third; set { _third = value; OnPropertyChanged( "Third" ); } }
        }
    }
  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3988 posts

    Posted 25 Sep Link to this post

    Hello, John,     

    RadGridView is capable of fetching bindable properties and data. However, during the data binding process, the grid extracts the data from the data source, but for any later changes in the data source, RadGridView should be notified. Your bindable collection and business objects should follow some standards established in .NET in order to notify RadGridView about the changes:
    - The collection that you will bind to RadGridView should implement IBindingList or IBindingListView interfaces. This will allow RadGridView to get notified about insert and delete operations of records.
    - Your business objects should implement INotifyPropertyChanged interface (.NET 2.0). This will allow RadGridView to reflect changes which occur to the properties of the business objects.

    Additional information is available here: https://docs.telerik.com/devtools/winforms/controls/gridview/populating-with-data/reflecting-custom-object-changes-in-rgv

    Once I change the DataSource collection to be BindingList<GridData>, the changes in the Timer.Tick event are immediately reflected to the grid.

    I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

    Regards,
    Dess | Tech Support Engineer, Sr.
    Progress Telerik

    Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).

Back to Top