How can I know when the items source has been changed on a data grid?

Apr 9, 2010 at 10:43 AM

I have a user control with a datagrid and navigation (first/prev/next/last page) buttons at the bottom. The datagrid is public so that other windows can change columns, subscribe to events on the grid etc. When the item source is changed on the grid I need to recalculate the pagesizes etc. However, I can't workout when the items source is changed on the grid. I've worked around it by adding an items source property on the user control. But it won't work if the item source is set directly on the data grid.

How can I know in my user control when the items source has been changed on the data grid?

Apr 9, 2010 at 4:23 PM

Hi Harshap,

You can try some of the above approaches:

  1. If you are using ItemsSource binding you can  set NotifyOnTargetUpdated = true for the binding and handle TargetUpdated event.
  2. Alternatively you can use the following code to check for udpates:
DataGrid myGrid = new DataGrid();
CollectionView myCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(myGrid.Items);
((INotifyCollectionChanged)myCollectionView).CollectionChanged += new NotifyCollectionChangedEventHandler(DataGrid_CollectionChanged);