Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid how to get when ItemsSource updates

Which event fires when DataGrid's source is updating? I've tried DataContextChanged and SourceUpdated but it never worked out.

Actually I need a simple thing. I want, if there is a new row comes, scroll the GridView's scrollbar down to the bottom to see what it was.

like image 651
iLemming Avatar asked Jul 10 '09 00:07

iLemming


3 Answers

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.

like image 86
Nidal Valot Avatar answered Nov 05 '22 03:11

Nidal Valot


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.

like image 8
rams Avatar answered Nov 05 '22 03:11

rams


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.

like image 3
Cody C Avatar answered Nov 05 '22 04:11

Cody C