Disable events temporarily

  • Thread starter Thread starter Marc Solé
  • Start date Start date
M

Marc Solé

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_CellValueChanged);

dgvItems.CellEndEdit += new
DataGridViewCellEventHandler(dgvItems_CellEndEdit);

dgvItems.CellValidated += new
DataGridViewCellEventHandler(dgvItems_CellValidated);

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_CellValueChanged);

dgvItems.CellEndEdit -= new
DataGridViewCellEventHandler(dgvItems_CellEndEdit);

dgvItems.CellValidated -= new
DataGridViewCellEventHandler(dgvItems_CellValidated);

dgvItems.RowEnter -= new DataGridViewCellEventHandler(dgvItems_RowEnter);

dgvItems.RowLeave -= new DataGridViewCellEventHandler(dgvItems_RowLeave);

dgvItems.RowValidated -= new
DataGridViewCellEventHandler(dgvItems_RowValidated);

}

}
 
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
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top