I am building a WPF application using C# in VS2010.
I have a database — lets call it mydatabase.mdf — and I have attached it correctly to my project and built a DataContext.
Now I have a ListView in my user interface that shows items from a specific table in this database, but the problem is that every time a new item is added to my database I want this ListView to update its items.
I have already tried:
listView.items.refresh()
but it didn’t work.
And I have tried something, namely applying a query on this database table every time a new item is added, and this query withdraws all the elements; then I do:
listview.itemssource = myquery
but it didn’t work either.
please help me pleaseeeeeeeeeeeeeee
The database can’t notify the client when a new item is added.
When its your client who adds the item to the database, an ObservableCollection will solve your problem. It implements INotifyCollectionChanged and notifies the UI about changes made to the collection (on the client).
When other clients write to the database as well, your only chance is to poll the database periodically (requery the database) and apply the delta (changes) to your ObservableCollection manually, or completely rebind the datasource (sortings etc. might get lost then) or refresh the ItemsCollection in case you don’t use an ObservableCollection or something implementing INotifyCollectionChanged.
Depending on your archicecture, a Remoting-Service could poll the database for all clients and then notify them all. That could be helpful if polling poses a bottleneck.