Observable Collection does not update datagrid

I use Dim All_PriceLists As System.Collections.ObjectModel.ObservableCollection(Of BSPLib.PriceLists.PriceListPrime)where PriceListPrimeimplements Inotify for all properties in it.

I attached All_PriceListto the datagrid as DataGrid1.ItemsSource = All_PriceLists, but when I do All_PriceLists=Getall()where Getall reads and receives data from the database, the datagrid is not updated.

It only updates when I crack it as follows:

DataGrid1.ItemsSource = Nothing
DataGrid1.ItemsSource = All_PriceLists

Could you tell me where I did wrong or what I have to implement. Thank.

+3
source share
3 answers

You have several solutions to your problem.

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

    DataGrid1.ItemsSource = new ObservableCollection(Of PriceListPrime)(GetAll())
    
  • Refresh ObservableCollection (as mentioned in another answer)

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

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

    INotifyPropertyChanged PropertyChanged Items. Xaml DataGrid ItemsSource Items.

+4

, , , . , . , , datagrid , .

, datagrid /datatable, .

+2

ObservableCollection , , .

, All_PriceList . :

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

ObservableCollection does not support AddRange, so you need to add items one at a time or implement INotifyCollectionChangedin your own collection.

+2
source

All Articles