Link to home
Start Free TrialLog in
Avatar of rajesh_khater
rajesh_khater

asked on

Is it safe to show a MessageBox from the DataGridView RowValidating event handler?

Is it safe to show a MessageBox from the DataGridView RowValidating event handler?

Some time back, I faced a situation where showing a MessageBox from the RowValidating event handler triggered another occurence of the RowValidating event handler.

My question is:
1. Does showing a MessageBox from the RowValidating event handler have any side effects ?
2. If Yes, how to code the RowValidating event handler to deal properly with those side effects (showing the MessageBox is a requirement for me).

Avatar of Nico
Nico
Flag of Netherlands image

It should be safe I guess. Keep in mind thought that pressing the OK button using the keyboard will raise an KeyUp event in the MessageBox' parent. At least, in my experience. Maybe your troubles have something to do with this?
Avatar of illusio
Showing the messagebox in the validating event shouldn't cause any problems.
Most of the problems come from what you do in response of the answer to the messagebox...
An important rule:
Never change the row or cell in the validating event! In general never change focus from within validating events. On top of that - don't change the content of the row in the validating event if you consider the input valid - only do that when you use the cancel = true. Cancelling will keep you inside the row (unless you changed the default behaviour for the validation (on form level change the value of the AutoValidate property) thus avoiding unnecessary events being fired.

Hope this helped,
Peter
SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rajesh_khater
rajesh_khater

ASKER

Well. showing a MessageBox in itself may change event flow. Just see my other question :

https://www.experts-exchange.com/questions/22709066/Button-click-event-does-not-fire-when-focus-is-in-a-DataGridView-row.html
Yes.  It may.  My description above was very specific to the situation in this question.  A button is a single control that has, or has not, focus.  A textbox is a single control that has, or has not, focus.  A datagridview appears to me to be a complex set of controls within which the internal focus can shift from control to control.  Empirically, the behaviour can be observed to be different.  Why, in precise technical terms, that should prove to be so I am, as I said, not qualified to say.  But I do not see that anything in your other question invalidates anything that I said above.

Roger
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial