473,499 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGridView firing way to many SelectionChangedEvents! Please Help

Hi all,

I'm really hoping someone can help me with this as it's causing me some
serious problems.

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

Best Regards

Simon
Aug 10 '08 #1
6 8715
On Sun, 10 Aug 2008 20:41:23 +0100, Simon Harvey
<no******@hotmail.comwrote:
>Hi all,

I'm really hoping someone can help me with this as it's causing me some
serious problems.

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

Best Regards

Simon
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.
Aug 10 '08 #2
Hi Jack,

Thank you ver much for your reply. I have been trying various
incarantions of these suggestions though I haven't gone as far as trying
to subclass the control.

Quick question. When you say:
"Clear DataSource before making changes and restoring it afterwards."
How do I do this exactly? At the moment I just get my datatable and then
assign it to my BindingSource's DataSource property. e.g.

tblPlaces = tableAdapter.SelectAll();
bsPlacesBindingSource.DataSource = tblPlaces;

This works but each time the number of SelectionChanged events that are
fired increases, which I don't understand. It's almost as though behind
the scenes the number of rows is quietly increasing each time, but that
doesn't appear to be the case because the grid continues to display the
correct number of rows (eventually!)

I'm wondering from your reply if there is a better way to refresh the
data that my bindingsourece or datagridview is using?

Many thanks again for your help

Simon
Aug 11 '08 #3
Hi Jack,

Thank you ver much for your reply. I have been trying various
incarantions of these suggestions though I haven't gone as far as trying
to subclass the control.

Quick question. When you say:
"Clear DataSource before making changes and restoring it afterwards."
How do I do this exactly? At the moment I just get my datatable and then
assign it to my BindingSource's DataSource property. e.g.

tblPlaces = tableAdapter.SelectAll();
bsPlacesBindingSource.DataSource = tblPlaces;

This works but each time the number of SelectionChanged events that are
fired increases, which I don't understand. It's almost as though behind
the scenes the number of rows is quietly increasing each time, but that
doesn't appear to be the case because the grid continues to display the
correct number of rows (eventually!)

I'm wondering from your reply if there is a better way to refresh the
data that my bindingsourece or datagridview is using?

Many thanks again for your help

Simon
Aug 11 '08 #4
On Mon, 11 Aug 2008 11:35:09 +0100, Simon <no******@hotmail.com>
wrote:
>Hi Jack,

Thank you ver much for your reply. I have been trying various
incarantions of these suggestions though I haven't gone as far as trying
to subclass the control.

Quick question. When you say:
>"Clear DataSource before making changes and restoring it afterwards."

How do I do this exactly? At the moment I just get my datatable and then
assign it to my BindingSource's DataSource property. e.g.

tblPlaces = tableAdapter.SelectAll();
bsPlacesBindingSource.DataSource = tblPlaces;

This works but each time the number of SelectionChanged events that are
fired increases, which I don't understand. It's almost as though behind
the scenes the number of rows is quietly increasing each time, but that
doesn't appear to be the case because the grid continues to display the
correct number of rows (eventually!)

I'm wondering from your reply if there is a better way to refresh the
data that my bindingsourece or datagridview is using?

Many thanks again for your help

Simon
My suggestion was to set the grid's DataSource to Nothing, make the
changes to the data source, then set the DataSource back to what it
was before. I don't use TableAdapters so I don't have any experience
with them.
Aug 11 '08 #5
OK - I'll give that a shot - thank you

Simon
Aug 11 '08 #6
Hi Simon,
I had a similar problem, my solution was something like

bool canDoThat = true;

private void dgv_SelectedChanged(object sender, Event e) {
if (!canDoThat) return;
canDoThat = false;
... // do whatever you need to do
canDoThat = true;
}

Note: it is not thread-safe solution. If you use threads, you would
need some locking on canDoThat variable, I guess.

Cheers
Ondra
--
http://xmedeko.blogspot.com
Aug 15 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3556
by: DraguVaso | last post by:
Hi, I'm using the DataGridView in VB.NET 2.0. The DataSource is a Generic List of a custom class0: lstMyPersonnes = New List(Of clsPersonne). When I add a new clsPersonne to lstMyPersonnes,...
6
6386
by: dbuchanan | last post by:
Hello, Is this a bug? Is there some kind of work around? I want to add default values for a few columns in my datagridview I found the "DefaultValuesNeeded" event for the datagridview I...
10
39444
by: Henok Girma | last post by:
Hello Gurus, I want to save the state of an unbound DataGridView on my Windows Form application to an XML file so i can later load it back.. Basically, on my form I have a DataGridView, it's got...
1
18774
by: Dave A | last post by:
Hi, I am struggling with two way databinding in WinForms and the DataGridView. I am binding to business object classes (rather than datatables). If I have a collection of these business...
0
1292
by: Paul Cheetham | last post by:
Hi, I have a DataGridView Control, which I am databinding at run-time. When the data is loaded, the first row in the grid gets selected, and the selectionChanged event gets fired. I want the...
2
24507
by: bob | last post by:
Can anyone tell me the best way to update a dataset while it is being edited/viewed in the DataGridView control? Is this something that should be inserted into one of the grid's events? or should...
7
12550
by: Mitchell S. Honnert | last post by:
Is there an equivalent of the DataGrid's DataGridTableStyle for the DataGridView? If not, is there an easy way to duplicate the DataGridTableStyle's functionality for the DataGridView? Here's...
3
2503
nirmalsingh
by: nirmalsingh | last post by:
hai all, i have added a dropdownlist control inside a datagridview by datagridview.controls.add method, now i want fire the event of dropdownlist, but when i try it, event of datagridview is firing....
2
1969
by: Ian Semmel | last post by:
If I can explain it. I have a DataGridView with say 100 rows, 30 of which will display on the screen. If I drag down the vertical scroll bar so that say row 50 is the centre row vertically...
3
5701
by: Andrus | last post by:
I have DataGridView in virtual mode containing 3500 rows. In code below, assigning to RowCount value to 3500 takes 8 seconds. CPU usage goes high at this time. Stepping by F11 into user code shows...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.