all groups > dotnet windows forms databinding > march 2008 >
You're in the

dotnet windows forms databinding

group:

Supress BindingSource/DataGridView SelectionChanged on new DataSou


Supress BindingSource/DataGridView SelectionChanged on new DataSou Jim Parsells
3/10/2008 11:53:27 AM
dotnet windows forms databinding:
(Framework 2.0, VS2005, XP sp2 all fully patched)
I have a Form with a DataGridView and a BindingSource, among other controls.
The DataSource of the DataGridView is the BindingSource. The DataSource of
the BindingSource is a List(Of myclass). So far, so good.

In response to user action on the other controls, the List(Of myclass) may
be re-populated. When this happens, I execute "BindingSource1.ResetBindings".
I do this as well when the List(Of myclass) is resorted based on a
ColumnClick on the DataGridView.
Again, so far, so good.

The Problem is that Resetbindings causes multiple SelectionChanged events
and multiple CurrentChanged events to be raised, though not necessarily an
equal number of each. I only wish to handle USER caused SelectionChanges (or
CurrentChanged) events.

In other words, I wish to supress or ignore such events that occur as a
side-effect of reloading/sorting the List(Of myclass). At the very worst, I
would like to set some property or change my code such that I only get ONE of
each event per reload of the List(Of myclass). I know how to deal with it if
only one such event is raised. Irregular multiple occurances are much more
difficult to deal with.

In the DataGridView FAQ, there are words to the effect that Event Raising
can be suppressed. I have no idea where to even look for how to do this.

Any help will be appreciated.
--
Re: Supress BindingSource/DataGridView SelectionChanged on new DataSou Marc Gravell
3/12/2008 8:17:10 AM
Why not simply set a flag while you call ResetBindings()?

Marc

Re: Supress BindingSource/DataGridView SelectionChanged on new Dat Jim Parsells
3/12/2008 1:04:01 PM
Did you notice that I said that ResetBindings causes multiple events to be
raised?
The point here is that if a single ResetBindings causes multiple, and not
identical, numbers of events (1 this time, 3 the next, 4 the next, 1 the
next), I have nothing to depend on to stop ignoring the events.
I could do something with Mousexxx or Click events, and ignore
SelectionChanged or CurrentChanged events, but simply supressing side-effect
events would be so much cleaner.
--
Jim Parsells


[quoted text, click to view]
Re: Supress BindingSource/DataGridView SelectionChanged on new Dat Jim Parsells
3/12/2008 1:16:00 PM
To be fair, I will note that using a combination of a flag and checking to
see if the underlying data element has REALLY changed actually reduces the
problem to almost manageable levels. However, there is a remaining hangup.
Specifically, if there is only one item in the List(Of myclass), it is
impossible to determine if it is selected as a side-effect or selected
because the user chose to do so. The ramifications go on and on.

I have worked around this, in a less than elegant way, but my original
question remains. I would prefer to have NO selection as a result of
ResetBindings.
--
Jim Parsells


[quoted text, click to view]
Re: Supress BindingSource/DataGridView SelectionChanged on new DataSou scott@lloydsoftware.com
4/11/2008 4:05:49 PM
Jim, the solution to your problem is this:
Handle the LeftMouseButtonUp event and check to see if the following
two conditions are true:
- the e.Handled parameter passed to your routed event handler is false
- the grid's SelectedItem is null

You can do something similar to for if the user presses enter after
arrowing around on the grid.

Hope this helped! I was having the same problem and found the above to
solve my problem.

Scott Lloyd
AddThis Social Bookmark Button