I have a database with a table that records data coming from various sources.
3 columns
‘Source’ ‘Value’ ‘Timestamp’
e.g.
source1 21 ’11:03′
source6 22 ’11:03′
source9 456 ’11:03′
The table is updated 2 to 3 times a second.
Each Value may or may not change.
Each ‘Source’ value is displayed in a seperate label or text box on the screen (not in a grid).
I need to find the best method to get this data to bind to WPF contols.
What type of object should I hold the data in Dictionary, DataTable etc.
What type of object is going to hold the data?
How do I bind my Label or TextBox to the value.
I query the sources 2 to 4 times a second.
The vast majority of the time the values in the database table won’t change
Often it is only one value that changes
Sometimes they all change
I only expect to have upto about 30 unique datasources.
Put your thinking caps on please.
Just by chance I’m working through a very similar problem. Like me, I am assuming that you are updating the data in a background thread.
Collections
Use MTObservableCollection instead of ObservableCollection to store the data. A ObservableCollection is normally used for data binding on stuff that can change, but it can’t be updated from a background thread. MTObservableCollection works for me, but it isn’t inheritently thread safe so be careful.
http://www.julmar.com/blog/mark/2009/04/01/AddingToAnObservableCollectionFromABackgroundThread.aspx
Models
Each object should implement INotifyPropertyChanged Interface. This will make them support data binding. Also, WPF allows you to update properties this way on brackground threads.
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx
Prefer Update over Clear and Add
Make sure you always update objects when you can. Don’t just clear the collection and reload it, that will be very expensive and cause adverse UI effects. Of course you can do individual adds and removes when needed.