Observable Collection is not updating the datagrid

observablecollection, vb.net, wpf

Solution

You have several solutions to your problem

Update the ItemsSource directly (instead of replacing the local member variable)

DataGrid1.ItemsSource = new ObservableCollection(Of PriceListPrime)(GetAll())

Update the ObservableCollection (as mentioned in another answer)

All_PriceList.Clear(); 
For Each item in Getall() 
    All_PriceList.Add(item) 
Next 

Set your DataContext to a view model and bind to a property of the view model

Dim vm as new MyViewModel()
DataContext = vm
vm.Items = new ObservableCollection(Of PriceListPrime)(GetAll())        

The view model will implement INotifyPropertyChanged and raised the PropertyChanged event when the `Items` property is changed. In the Xaml your DataGrid's `ItemsSource` will bind to the `Items` property.

Problem

I am using a `Dim All_PriceLists As System.Collections.ObjectModel.ObservableCollection(Of BSPLib.PriceLists.PriceListPrime)` where `PriceListPrime` implements Inotify for all properties in it. I bound the `All_PriceList` to a datagrid as `DataGrid1.ItemsSource = All_PriceLists` but when I do `All_PriceLists=Getall()` where Getall reads and gets the data from the DB, the datagrid is not updating. It updates only when I hack it this way: ``` DataGrid1.ItemsSource = Nothing DataGrid1.ItemsSource = All_PriceLists ``` Could you please tell me where I have gone wrong or what I should implement. Thank you.

Original source