Hello,
I have a form with a grid, an Ok button and a Cancel button. Let's say the user is editing a cell and the current text is invalid. If the user clicks Ok there's a validation error and the grid keeps the focus. I can get that kind of behavior with no problem. Now if the user clicks Cancel I don't want any error message. Instead I want it to discard the current text in the cell and restore the original value, also the grid should give up the focus.
There are three issues here:
1) Identifying whether to display the error message or not: I tried to do this by setting the CausesValidation property of the Cancel button to false; for the Ok button I set it to true. I know the grid is not using the Validating and Validated events (see here) to trigger its internal validation, but my idea was detecting when the Validating error triggers (that happens when clicking the Ok button only) and only then the error message is displayed. The problem is when there's an invalid text the grid's Error event triggers before the Validating event, so I have no chance to determine validation is required. Instead I would expect the Validating event to trigger before the Error event. Is that a bug or that is working as intended?
2) I still havent' found a way to discard the current text in the cell and restore the original value (the one before starting editing). There has to be a way to do it programmatically because the user can do the same when hitting ESC.
3) Assuming there's a way to do 2), is there a way to handle the Error event so that the grid gives up the focus?
Thanks for the answer Andre. That's exactly what I needed.
Hi,
I stumbled upon the same problem. We have an UltraGrid where the contents of each cell needs to be validated immediately. The validation is currently performed in the BeforeExitEditMode event handler.
Now we want to use the CausesValidation=false property to realize a Cancel button. Following workaround seems to work:
All grids in our project had ExitEditModeOnLeave=true (default). I changed this to ExitEditModeOnLeave=false and added the following override in our control (which has UltraGrid as a base class):
virtual void OnValidating(CancelEventArgs^ e) override { PerformAction(Infragistics::Win::UltraWinGrid:: UltraGridAction::ExitEditMode); __super::OnValidating(e); }
As far as I see, this simply moves the Before-/AfterExitEditMode events from the Leave event to the Validating event. And now setting CausesValidation=false on the Cancel button allows us to leave the dialog even if the grid contains invalid data.
Thanks for the tips Mike.
Changing the UpdateMode is not a good option in my case. Most likely the developer will forget to call UpdateData when clicking the Ok button.
While looking for a solution I ran into the InvalidValueBehavior property of the Override. If I set it to RevertValueAndRetainFocus I obtain something close to what I want.
If the user clicks either Ok or Cancel the value is reverted and the cell retains the focus. That't fine for the Ok button. For the Cancel button it is odd that the form is not closed and the focus remains in the cell, but since the value was reverted he can just click Cancel again to close the form.
The grid automatically updates the current cell when it loses focus by default. But this is based on the UpdateMode property. So you could set UpdateMode to a setting that does not include LostFocus. This will meant that the grid doesn't update on a lost focus, though, so you will need to call UpdateData on the grid in your OK button click and anywhere else that you need to make sure the data is updated.
To simulate cancelling (as though the user pressed ESC), you can call the CancelUpdate method on the row. In this case, you would probably just use the ActiveRow.