Click here to Skip to main content
15,039,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have validation logic in the RowValidating event of a DataGridView. However, when a user clicks on a cell in a different row, it never triggers the RowValidating event from the row that was left. I don't really object to the moving to another row, I just want the original row to be validated!

The event triggers properly if a keystroke causes the row to change, but clicking another row with the mouse does not trigger the event. Is there a way to get the event to fire when the user clicks on another row?

What I have tried:

I've tried putting code in the RowLeave event to test if the row's been validated, but there doesn't seem to be anything to detect that. Also, I need the validation code to allow cancellation, so I cannot just put the validation code in RowLeave.
Posted
Updated 23-Jun-21 9:28am
Comments
Gerry Schmitz 23-Jun-21 13:05pm
   
That is why I usually use a (pop up) form if I need any validation when entering / update entries in collections. Otherwise, you need to maintain some sort of state that you check when moving to a new row (i.e. was there a validation pending on row x; validate it now, and go back or continue). Or you trap the mouse with validation pending; but then you start tying the user's hands, etc.
Maciej Los 23-Jun-21 14:36pm
   
Seems you need to use Validated Event[^] instead of Validating.
James Blair 2021 23-Jun-21 15:22pm
   
Validated doesn't allow me to back out of the change if validation fails.

1 solution

I ended up doing the following:

1. Saved the row index I was editing in the CellBeginEdit event. I needed this for item #2.

2. In the RowLeave event, force RowValidating on the saved row. This solved the basic problem except the situation noted in item #3.

3. In the CellLeave event, force edit mode on that cell to end. If I didn't do this, the CellEndEdit event didn't fire when clicking on another row while edit mode was active.
   
Comments
Maciej Los 24-Jun-21 2:21am
   
Great! You've solved your own issue.
A 5!
BTW: It would be great to see your question resolved. So, please, accept your own solution (green button).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900