WPF DataGrid how to get when ItemsSource updates WPF DataGrid how to get when ItemsSource updates wpf wpf

WPF DataGrid how to get when ItemsSource updates


I had the same problem and I manage it this way

DataGrid myGrid = new DataGrid();CollectionView myCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(myGrid.Items);((INotifyCollectionChanged)myCollectionView).CollectionChanged += new NotifyCollectionChangedEventHandler(DataGrid_CollectionChanged);

You then need to implement the logic in the event handler DataGrid_CollectionChanged.


Set NotifyOnTargetUpdated = true for the ItemsSource binding and handle TargetUpdated event. If you've multiple bindings, then look for DataTransferEventArgs Property to find out if the target is ItemsSource or not.


If you are trying to have the grid refresh when something is added to the database itself, that's not going to happen. I'm more familiar with WinForms than WPF but I'm assuming there is no magical way to keep a grid in sync with the database without writing some background process that continuously checks for database changes.

If you are updating the actual data source of the grid (ex. Collection) then that will update the grid.