skip to content »

Cellvalidating in datagridview in c

cellvalidating in datagridview in c-24

I think in the end it depends on what your validation needs are.

When this event is canceled in virtual mode, the Cell Value Pushed event will not be raised.I am able to validate my value properly but when I try to change it to something valid I get a Data Error. You should never attempt to change the cell value in the handler for this event, because unless you cancel the event (in which case the user is stuck in edit mode), the cell value is set to the value from the editing control immediately after the event finishes.This, therefore, undoes any action you perform in the handler.EDIT The biggest thing about the Cell Validating event is that you can stop the user from leaving a cell, if the value entered does not pass your validation. column, its value is tested for validity by checking that it is not empty. On the other hand, the Cell Value Changed event seems to trigger only when the value of the cell changes, which means the validation code runs only when the value changes and not everytime a user changes cells.

Now, since so many books use the Cell Validating event, I wonder if there is not any gotcha (in display for example) with using the Cell Value Changed?

I understand that the impact in performance should be irrelevant when using simple validation and conditional highlighting rules but I would as much prefer it not to run useless code everytime the user moves to another cell if it can be avoided.

Thanks, I'm using Cell Value Changed currently on a grid with custom validation and have had no problems with display or anything else.

If the event handler for the Cell Validating event finds that the value is an empty string, the Data Grid View prevents the user from exiting the cell until a non-empty string is entered.

For a complete explanation of this code example, see Walkthrough: Validating Data in the Windows Forms Data Grid View Control.

These events are not fired when the user cancels edit mode, which is good. They do almost what I want but they are also fired when the user cancels edit mode by pressing ESC.