Search code examples
c#winformsevent-handling

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.


Solution

  • 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 not 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).