Hi, i am facing problem related to the xamdatagrid it throws above exception. I have attached example to reproduce the issue , I am performing the same thing in my application except in my example in Load method i am adding data into the collection which is attached to the grid, in actual application load is my search method which returns me the collection and then i fill up the collection which is binded to the xamdatagrid.
Could you please look into this and let me know your inputs on the same.
Thanks
Prashant C
Hi Prashant,
Is there anything I am supposed to do to produce the exception? I ran your sample but I'm not seeing any exceptions and the data is loaded into the grid. If I am supposed to do something else please let me know.
Oh Sorry, After running the application click on ok and you will get the exception
That exception is occurring because the DataSource is being updated from a background thread. If you look at the full exception message it says: "XamDataGrid does not support changes (other than INotifyPropertyChanged.PropertyChanged or ListChangedType.ItemChanged) to its DataSource from a thread different from its Dispatcher thread." Your People collection is an ObservableCollection and this collection type does not use ListChangedType.ItemChanged.
Ultimately, data that is bound to a UI component should not be updated in a background thread as a change notification can trigger an update of the UI component which will take place on a different thread from the one that owns the control. Controls should not be updated from a background thread. If you need to update data in a background thread, update the data but do not trigger any notifications until after the thread has finished and you are back on the main thread.
Hi Rob,
I am also facing the same issues. I am trying to add items to Observable collection. Its giving me same exception "XamDataGrid does not support changes (other than INotifyPropertyChanged.PropertyChanged or ListChangedType.ItemChanged) "
Can you please provide me with the sample on how to resolve that.
Hi Asha,
The solution is to not add items to the ObservableCollection<T> from a background/worker thread if the collection has been bound to the XamDataGrid. It must be done on the UI thread. If you have to you can use the Dispatcher.BeginInvoke() method to force the code to run on the UI thread:
xamDataGrid1.Dispatcher.BeginInvoke(new Action(() =>{ myObservableCollection.Add(item);}));
If this is at startup then you should just wait till the background operation is finished and all the items have been added to the collection before binding it to the XamDataGrid.