sfDataGrid doesn't show data immediately, need to manual sort first

I'm new to the sfDataGrid. It looks very interesting.
I looked at the demo and its code that is shipped with v16.1.0.24

I dragged the control on my form and added in my form constructor:
            _order = new Order();
            sfDataGrid1.DataSource = _order.OrderLines;

I use _order.OrderLines.Add(SqliteService.ProductToOrderLine(product)); to add a new row to the DataGrid.

My classes:
    public class Order : IDisposable
    {
        public int Id { get; set; }
        public DateTime CreatedAt { get; set; }

        public double TotalPrice { get; set; }

        public IList<OrderLine> OrderLines { get; set; }

        public Order()
        {
            CreatedAt = DateTime.Now;
            OrderLines = new List<OrderLine>();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool isdisposable)
        {
            OrderLines?.Clear();
        }
    }

    public class OrderLine : INotifyPropertyChanged
    {
        private double _price;
        private string _name;
        private int _id;
        private int _productId;
        private string _barcode;
        private int _amount;

        [Display(AutoGenerateField = false)]
        public int Id
        {
            get { return _id; }
            set
            {
                if (value == _id) return;
                _id = value;
                OnPropertyChanged(nameof(Id));
            }
        }

        public int ProductId
        {
            get { return _productId; }
            set
            {
                if (value == _productId) return;
                _productId = value;
                OnPropertyChanged(nameof(ProductId));
            }
        }

        [Display(Name = "Name")]
        public string Name
        {
            get { return _name; }
            set
            {
                if (value == _name) return;
                _name = value;
                OnPropertyChanged(nameof(Name));
            }
        }

        [Display(Name = "Prijs")]
        public double Price
        {
            get { return _price; }
            set
            {
                if (value.Equals(_price)) return;
                _price = value;
                OnPropertyChanged(nameof(Price));
            }
        }

        public string Barcode
        {
            get { return _barcode; }
            set
            {
                if (value == _barcode) return;
                _barcode = value;
                OnPropertyChanged(nameof(Barcode));
            }
        }

        [Display(Name = "Aantal")]
        public int Amount
        {
            get { return _amount; }
            set
            {
                if (value == _amount) return;
                _amount = value;
                OnPropertyChanged(nameof(Amount));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

I tested OnPropertyChanged() and it is called.
But somehow sfDataGrid doesn't get notified.

When I sort the data the new data is shown. So the datasource is correct. It is just not updated.

What do I need to do more to tell sfDataGrid is should re-render. 


4 Replies

PM Paul Meems March 25, 2018 08:38 AM UTC

I'm calling sfDataGrid1.View.Refresh(); and now all is working as expected.


FP Farjana Parveen Ayubb Syncfusion Team March 26, 2018 12:00 PM UTC

Hi Paul,        
 
Thanks for contacting Syncfusion support. 
 
SfDataGrid allows to add a new row from the DataSource in run time. Since you are binding the List, List does not implement the INotifyCollectionChanged. So, the data is not notified to SfDataGrid. When we set the DataSource of the SfDataGrid with a collection that implements the INotifyCollectionChanged interface then the SfDataGrid automatically refreshes the view. Refer the below code example in which the SfDataGrid is set with an ObservableCollection as DataSource. The ObservableCollection implements the INotifyCollectionChanged interface and hence will result in automatic refreshing of view when adding rows in runtime. No need to call sfDataGrid1.View.Refresh(), because it will hit the performance. 
 
Code Example: 
 
 
public ObservableCollection<OrderInfo> OrdersListDetails 
{ 
    get { return _ordersListDetails; } 
    set { _ordersListDetails = value; } 
} 
 
 
public Form1() 
{ 
    InitializeComponent(); 
    var data = new OrderInfoCollection(); 
    sfDataGrid.DataSource = data.OrdersListDetails; 
} 
 
 
  
Regards, 
Farjana Parveen A 



PM Paul Meems April 12, 2018 08:32 AM UTC

Sorry for the late response. 
Your suggestion about the ObservableCollection works fine.

Thanks for the help. This question can be closed.


SN Sindhu Nagarajan Syncfusion Team April 13, 2018 12:27 PM UTC

Hi Paul, 

Thanks for the update. 

We are glad to hear that the provided solution resolved your scenario. Please let us know if you have any other queries. 

Regards, 
Sindhu 


Loader.
Up arrow icon