3

We have a Infragistics xamDataGrid with the DataSource bound to a BindingList. We have some custom text set on the group by header and need to reset it every time an item is added or deleted.

Is there a when to be notified when an item is deleted in the DataSource so that we can update the group by header? The insertion is easy as it has a InitializeRecord event which we can use to refresh the group by header. We're looking for an equivalent for deletion.

Just to note, the BindingList is NOT updated in the GUI so we cannot use the RecordsDeleting and RecordsDeleted events offered by xamDataGrid.

Daisuke Shimamoto
  • 5,206
  • 6
  • 32
  • 37

1 Answers1

2

You can use the same event that the grid you are setting the data source to uses if the data source is an IBindingList: the ListChanged event. Alternatively if your data source also implements INotifyCollectionChanged you can use the CollectionChanged event.

To do this you just subscribe to the event in the same section of code that sets the data source. Something like:

dataSource.ListChanged += DataSource_ListChanged;

and you'll get this info in your handler:

which is described like this:

The NewIndex property indicates the index of the item that was added, changed, or deleted. If an item was moved, the NewIndex property indicates the new location of the item and the OldIndex property indicates the old location.

Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
  • Thanks Rick. Do you think there's a way to avoid setting things in the BindingList and purely handle it in the view (may be detabable but the header is a view thing)? Just wanted to see if there's a cleaner way. – Daisuke Shimamoto May 31 '11 at 07:28
  • 1
    @Daisuke: Utilizing events generated by the view-model from the view **is** part of the view. Change notification is an essential part of the foundation of all data binding. It's there expressly to be used by the view. That being said, sometimes the change notification is "repackaged" and made available to other parts of the view. That's OK too, if those events meet your needs. But if they don't, do not hesitate to use the change notification mechanism for your own purposes as we've done in this question. It enables you to get the events you need without sacrificing loose coupling! – Rick Sladkey Jun 01 '11 at 01:08