views:

121

answers:

2

Hi,

When i use DataGridComboBoxColumn in my WPF DataGrid, the DataGrid SelectionChanged event is triggering multiple times based on the number of rows while loading the rows in the WPF DataGrid.

How can i stop this? Because of this I am facing Performance issue.

A: 

It depends on exactly how you have your bindings set up.

If for instance you have an ObservableCollection and you add items to it when you load data, you might run into this kind of problem. There are multiple solutions but I'd rather pinpoint the problem than typing kilometers of text, so if you can provide a bit more details I'll reply with my best guess at a solution.

Edit: After seeing the sample I figured out what the problem is: there's a DataGridComboBoxColumn in the DataGrid, with a SelectedValue binding to a property; when the binding is executed, the SelectionChanged event of the ComboBox is fired, and is caught by the handler on the DataGrid. There are several options to prevent this... one is to check the OriginalSource in the EventHandler, and the other is to handle the event on the ComboBox and to set its Handled property to true so it doesn't get caught by the DataGrid handler as well.

An alternative, much better solution would be to not handle the selection events in the code-behind unless there's a very solid reason. It's best to bind the ItemsSource of the DataGrid to an ICollectionView (ListCollectionView for example) which represents the original collection; the ICollectionView's CurrentItem is automatically synchronized with the selected row in the DataGrid and you can handle selection changed events on the ICollectionView, making it much easier (and unit-testable, somewhat separate from the UI implementation etc.). This doesn't work with multiple selections, but if you can only select a single row at a time it should work quite well.

Alex Paven
Hi Alex, I created a sample and uploaded under the following path. Hope it gives more information. http://cid-ab034720ad5419bf.office.live.com/self.aspx/.Public/ksvimal%20files/DgrSelChngMultiTrigg.zip
ksvimal
Thanks for you feedback. It works :-)
ksvimal