I have a xamDataGrid bound to ObservableCollection. I am using MVVM pattern. New items are added to the ObservableCollection by an event trigger implemented in the underlying VM class.
I am using:
App.Current.Dispatcher.Invoke(new Action(() => { _tradeSummary.Add(item); })); // _tradeSummary is the ObservableCollection
This works fine and I see the new Row added in the xamDataGrid.
However, when existing data items are changed in the ObservableCollection, I just can't get the existing rows in xamDataGrid to update. I tried using the same App.Current.Dispatcher.Invoke() and Notify(), but didn't work.
Please help.
P.S. If I put my data in a DataTable and bind to DataView, the existing data updates fine but then then new data rows don't show in the grid.
Hello Jay, Thank you for the code-snippet you have provided. In order for the XamDataGrid to update accordingly whenever you have changed the DataItems of the DataSource, you will need to implement the INotifyPropertyChanged interface for your DataItem class.
public string vmName{ get { return this.Name; } set { this.Name = value; // Comment to notice how the XamDataGrid // will NOT get updated. NotifyPropertyChanged("vmName"); }}
Thanks for the reply. I think, I am implementing the INotifyPropertyChanged interface the way you have mentioned. However, the collection is updated by an event that my class subscribes to. There are no methods and ICommand statements.
Shouldn't just firing NotifyPropertyChanged("vmName"); cause the entire grid to update. Note - as I mentioned, this works when new rows are added but not when existing data is changed.
Hello Jay, When using an ObservableCollection and implementing the INotifyPropertyChanged for the DataItems inside, the XamDataGrid's respective records should be successfully updated when using either a command or an event handler. The NotifyPropertyChanged method is responsible for notifying the grid when a particular DataItem's property has been changed. It does not affect the entire XamDataGrid, because certain properties might not use the method. Would you please modify the sample from my previous reply, so the issue is reproduced and send it back to me? Having this information will help me further investigate this matter for you. Looking forward to hearing from you.
I made the changes to your project and it still worked fine. I think, I had implemented each property within my collection as Prop {get; set} without the INotifyPropertyChanged trigger within Set. I think, that should be it - will test tomorrow.
Thanks.
Hello Jay, Thank you for your feedback. If you have any other questions in particular, please let me know.
No thanks - the issue was with me not having Notify event for each individual data class property.
Hello Jay, I am glad to know that you were able to achieve the functionality you were looking for.