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

GridView CRASH when data source changed under hierarchical view

1 Answer 58 Views
GridView
This is a migrated thread and some comments may be shown as answers.
w
Top achievements
Rank 1
w asked on 19 Apr 2017, 06:13 AM

Purples: 

    I have a master-details gridview, but the data should be refreshed when the source data is changed. I implemented INotifyPropertyChanged interface for the model class. However, when I use the hierarchical gridview, when the binding data is changed, the gridview could NOT always work fine.

The attached is my test program.

The first level is Product, the child level is its orders.

I have a background thread to update the product and orders.

I am using 2017.1.221.40.Trial.

 

CODE below:

----------------------------------------------------------------------

 public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        BindingList<ProductData> _tradeProdDataList = new BindingList<ProductData>();      
        public RadForm1()
        {
            InitializeComponent();
            GenenateData();
            this.radGridView1.DataSource = _tradeProdDataList;
           
            this.radGridView1.AutoGenerateHierarchy = true;
        }
        
        private void GenenateData()
        {
            for (int i = 0; i < 10; i++)
            {
                var prod = new ProductData() { ID = i };
                _tradeProdDataList.Add(prod);
            
                var list = new List<OrderData>();
                for (int j = 0; j < 10; j++)
                {
                    var o = new OrderData() { ProductID = i, OrderName = string.Format("Test-{0}-{1}", i, j) };
                    list.Add(o);
                }
                prod.Orders.AddRange(list);
            }
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            var t1= Task.Factory.StartNew(new Action(() =>
            {
                while (true)
                {
                    _tradeProdDataList.ToList().ForEach(p =>
                    {
                        p.Quantity = new Random().NextDouble() * 1000.0;
                        p.Orders.ToList().ForEach(o =>
                        {
                            o.LastPx = new Random().NextDouble() * 1000.0;
                        });
                    });
                }
            }));
        }
    }
    

    public class ProductData : INotifyPropertyChanged
    {
        public ProductData()
        {
            Orders = new List<OrderData>();
        }
        public int ID { get; set; }
        private double _Quantity;
        public double Quantity { get { return _Quantity; } set { _Quantity = value; NotifyPropertyChanged(); } }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "none passed")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public List<OrderData> Orders
        {
            get; set;
        }
    }

    public class OrderData : INotifyPropertyChanged
    {
        public int ProductID { get; set; }     

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "none passed")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private string _OrderName; public string OrderName { get { return _OrderName; } set { _OrderName = value; NotifyPropertyChanged(); } }

        private double _LastPx;
        public double LastPx { get { return _LastPx; } set { if (value != _LastPx) { _LastPx = value; NotifyPropertyChanged("LastPx"); } } }

    }

 

 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Hristo
Telerik team
answered on 19 Apr 2017, 08:40 AM
Hi,

Thank you for writing.

Updating your data source collection from a background thread would result in an exception. The updated data source will trigger an update in the grid and since you are doing it on a thread different than the UI thread, you need to invoke the control.

Please check the following MSDN article providing detailed information and examples handling a similar scenario: https://msdn.microsoft.com/en-us/library/ms171728(v=vs.110).aspx.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
w
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Share this question
or