On Sun, 10 Aug 2008 20:41:23 +0100, Simon Harvey
>I'm really hoping someone can help me with this as it's causing me some
>I have a Windows Forms application using the gridview control. When the
>user selects a row, the SelectionChanged event fires and I load some
>more data that's particular to that row.
>The problem I'm having is that the SelectionChanged event is going way
>to many times and this is obviously causing me to load the additional
>data scores of times when it's not necessary.
>The sorts of things that are causing it to occur are:-
>- Refreshing the data
>- Calling ClearSelection
>- Calling Sort
>I can certainly understand why these actions would cause the
>SelectionChanged event to fire, but the problem is the sheer number of
>times it fires. For example for some reason, when I refresh the binding
>source's data, it will fire say, 5 times. Then if I do it again, it will
>become 10 times even though I'm clearing the data and not adding to it.
>Similarly, when I call sort it seems to cause SelectionChanged to fire
>once for each row.
>I've tried all sorts of things like detaching event handlers and then
>reattaching them after performing a key action but this only helps a
>little. Sometimes detaching the event handler doesn't seem to do
>anything - the event still fires!
>Can anyone help me with some strategy for dealing with this. I'd be
>really surprised if I were the only person that was experiencing this so
>I'm hoping someone will be able to help me.
>Sincerest thanks to anyone who can advise
I assume you mean the DataGridView control. I don't know of any good
solution to this.
Some things I have done to mitigate this are:
Use a subclassed DataGridView. Override the Sort and OnCellClick
methods to determine when the grid is sorting, and in
OnSelectionChanged don't call the base class method to prevent the
SelectionChanged event from occurring.
Clear DataSource before making changes and restoring it afterwards.
Subclass the DataGridView and put code in OnSelectionChanged to try to
notice redundant calls.
Subclass the DataGridView and add a method to set/reset a flag. Call
it to disable the events before making changes and call it to enable
after. Override OnSelectionChanged and if the disable flag is set
don't call the base class OnSelectionChanged.
If the event handler is detached, it is not possible for the event
handler to be called. My guess is either you don't have the detach
and attach paired so that you have it attached more than once, or the
grid is raising the event after you reattach. If you put a breakpoint
on the handler and look at call stack window you should be able to
tell what raised the event. Events occurring later than you expect is
a big problem when the grid is first being instantiated - many grid
events are delayed until the grid becomes visible.