Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a treeview in a WPF application that links to a SQL Express DB using Linq to SQL and a data provider. Simple and it works.

treeMovies.ItemsSource = dataProvider.GetCollection_Movies();

My problem is that I can find no way to refresh the treeview after a new record is added to the DB with out killing the itemsource and re-adding it. Is there a way to refresh the treeview to show the new records with out reloading the entire DB?
thanks
Scott
Posted

1 solution

Use an ObservableCollection (OC) as your ItemsSource. When you add something to the DB, add it to the OC. When you delete from the DB, delete from the OC. When you modify a record in the DB, either replace or modify the item in the OC. If you modify the item (e.g., change properties on it), then you'll want to make sure that item is observable as well (one way to do this is to implement the INotifyPropertyChanged interface).
 
Share this answer
 
Comments
ScottRadel 11-Dec-10 10:57am    
I have not taken a look at the observablecollection yet as I am pretty new to WPF. As I want to change the query in the treeview at runtime I assume I will need to use it in code behind. I will see what I can find on the internet, but if anyone knows of a good tutorial please post it.
thanks
Scott
AspDotNetDev 11-Dec-10 19:06pm    
Check out this article: http://www.codeproject.com/KB/silverlight/SLListVsOCollections.aspx . That is Silverlight specific, but Silverlight and WPF share most of their technologies, including ObservableCollection and INotifyPropertyChanged.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900