简体   繁体   中英

How to cancel an event without using e.Cancel method?

I want to cancel the Data gridview cell click event

private void gridViewHistory_CellClick(object sender, DataGridViewCellEventArgs e)
{      
}

but this eventargs doesn't have cancel event, How to cancel that event ? May be this question has a very easy answer but still i am stuck it in, needs help.

Thanks in advance.

You can't.

WinForms events are just multicast delegates then they can't be canceled (in the sense of stop propagation of the event ) unless this situation is handled by the object that exposes the event (but I'm now aware of any object that supports this, usually events are notifications).

That said, some events has argument with a Cancel property, it's used by the object to cancel the action that should be performed because of that event. Again if the implementation does not provide that property there is not anything you can do to change this behavior.

In your case you should override the OnCellClick method of DataGridView to handle that in the way you prefer (if you do not call the base class then cell won't get the click event and CellClick event won't be fired).

Not all events can be cancelled. You can choose to not do anything in your event handler but that doesn't mean you can cancel it.

So, unless there is a specific Cancel type method you Cannot cancel the event.

You may want to use CellValidating event:

private void gridViewHistory_CellValidating(object sender,
    DataGridViewCellValidatingEventArgs e)
{
    if (.....)
    {
        e.Cancel = true;
    }
}

You can try the

return;

statement, but this only works for asp:LinkButton OnClick events that do not then pass over to other post events, such as an UpdateCommand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM