471,594 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Disable events temporarily

Hello everybody,

I have a little "problem".

I have a DataGridView that is loaded automatically by code sometimes, and
manually by the user other times.

When the user introduces data in the DataGridView, there are some events
that control the introduced data for any errors the user can do. That's OK.
But when the data is loaded automatically, it is supposed that this data is
correct, so I don't need that these events are called.
I want to do that because with a lot of data, the load is very slowly, and
with the events disabled it takes only few seconds.

Anyone can help me, and tell me how can I disable these events?

I have done that, but it doesn't work.

Thanks in advance!

Marc

private void EventDisabler(bool Enable)

{

if (Enable)

{

//Enable event handler

dgvItems.CellClick += new DataGridViewCellEventHandler(dgvItems_CellClick);

dgvItems.CellValueChanged += new
DataGridViewCellEventHandler(dgvItems_CellValueCha nged);

dgvItems.CellEndEdit += new
DataGridViewCellEventHandler(dgvItems_CellEndEdit) ;

dgvItems.CellValidated += new
DataGridViewCellEventHandler(dgvItems_CellValidate d);

dgvItems.RowEnter += new DataGridViewCellEventHandler(dgvItems_RowEnter);

dgvItems.RowLeave += new DataGridViewCellEventHandler(dgvItems_RowLeave);

dgvItems.RowValidated += new
DataGridViewCellEventHandler(dgvItems_RowValidated );

}

else

{

//Disable event handler

dgvItems.CellClick -= new DataGridViewCellEventHandler(dgvItems_CellClick);

dgvItems.CellValueChanged -= new
DataGridViewCellEventHandler(dgvItems_CellValueCha nged);

dgvItems.CellEndEdit -= new
DataGridViewCellEventHandler(dgvItems_CellEndEdit) ;

dgvItems.CellValidated -= new
DataGridViewCellEventHandler(dgvItems_CellValidate d);

dgvItems.RowEnter -= new DataGridViewCellEventHandler(dgvItems_RowEnter);

dgvItems.RowLeave -= new DataGridViewCellEventHandler(dgvItems_RowLeave);

dgvItems.RowValidated -= new
DataGridViewCellEventHandler(dgvItems_RowValidated );

}

}

Nov 3 '06 #1
4 16327
I take it by "it doesn't work" you mean that they are still firing.
What you have should work, if used correctly. Is it possible that it has
been enabled twice (without an unsubscribe) through some route? As if so, it
will be subscribed twice, and fired twice. This will a: be slower, and b: if
you unsubscribe (-=) it will only remove the first found instance, perhaps
leaving one behind.

If this is the case, I would sugggest either keeping a bool field to track
if you think you are currently subscribed, or, *always* run the -= (which
unsubscribes if possible), then (if enabled) run the +=, so you can only
ever have 1 instance of each. The bool is cleaner.

Marc
Nov 3 '06 #2
Thanks Marc,

I thought before to do that with the bool field, but it will always rise the
event and then compare the bool, isn't it?

Finally I choose the option to run always the -= .

Thanks a lot for your help.

Marc
"Marc Gravell" <ma**********@gmail.comescribió en el mensaje
news:e0**************@TK2MSFTNGP04.phx.gbl...
>I take it by "it doesn't work" you mean that they are still firing.
What you have should work, if used correctly. Is it possible that it has
been enabled twice (without an unsubscribe) through some route? As if so,
it will be subscribed twice, and fired twice. This will a: be slower, and
b: if you unsubscribe (-=) it will only remove the first found instance,
perhaps leaving one behind.

If this is the case, I would sugggest either keeping a bool field to track
if you think you are currently subscribed, or, *always* run the -= (which
unsubscribes if possible), then (if enabled) run the +=, so you can only
ever have 1 instance of each. The bool is cleaner.

Marc

Nov 3 '06 #3
No, I meant in your method... i.e.

bool eventsBound = false;
void BindEvents(bool enable) {
if(enable && eventsBound) return; // nothing to do; already bound...
if(!enable && !eventsBound) return; // nothing to do; not bound...

if(enable) {
someControl.SomeEvent += someHandler;
someControl.SomeOtherEvent += someOtherHandler;
} else {
someControl.SomeEvent -= someHandler;
someControl.SomeOtherEvent -= someOtherHandler;
}
eventsBound = enable;
}

Marc
Nov 3 '06 #4
Thank you very much for your help.

It works fine now.

Marc
"Marc Gravell" <ma**********@gmail.comescribió en el mensaje
news:em**************@TK2MSFTNGP03.phx.gbl...
No, I meant in your method... i.e.

bool eventsBound = false;
void BindEvents(bool enable) {
if(enable && eventsBound) return; // nothing to do; already bound...
if(!enable && !eventsBound) return; // nothing to do; not bound...

if(enable) {
someControl.SomeEvent += someHandler;
someControl.SomeOtherEvent += someOtherHandler;
} else {
someControl.SomeEvent -= someHandler;
someControl.SomeOtherEvent -= someOtherHandler;
}
eventsBound = enable;
}

Marc

Nov 3 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dw70 | last post: by
8 posts views Thread by ORC | last post: by
4 posts views Thread by nick | last post: by
4 posts views Thread by Jon Slaughter | last post: by
10 posts views Thread by Gary Jefferson | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.