Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
in my WinForms app. i have master-detail DatagridViews.
i have a RowValidating and CellValidating event handlers on detail grid.

if detail cell/row validation fails, i set e.Cancel to TRUE.
this prevents user from leaving cell/row in details grid, EXCEPT in one case :

when user inserts invalid data in details cell and directly moves focus to master grid to a record in which is not its parent, then user is not prevented from leaving that invalid cell !

further more, when i return back to THAT invalid cell, i discover that invalid value wasn't accepted, and cell has its old value.

another case: if i delete cell's value instead of inserting an invalid value, e.Cancel is also set to TRUE because that value is required. i repeat the previous scenario and when i get back to that cell, i find it EMPTY, not even having its old value...

can anyway tell me how to prevent user from leaving invalid detail cell ?
Posted
Updated 1-Oct-14 22:28pm
v2
Comments
Sergey Alexandrovich Kryukov 1-Oct-14 13:00pm    
The behavior is wrong, because you implemented it on a wrong way? But how can we know what's wrong? A comprehensive but minimized code sample could help.
—SA
nina4ever 2-Oct-14 3:54am    
thanks for replying.
validation works in other cases. for example if the detail cell is empty or has an invalid value, and user moves focus to a textbox for example, then: e.Cancel = true, and user is prevented from leaving cell.

which code shall i post?
master-detail grids creation?
detail grid CellValidation?
detail grid RowValidation?


here u can find my master-detail DGV code. it is posted in the answer, not in the question

http://www.codeproject.com/Questions/817587/Master-detail-datagridview-how-to-insert-master-re?arn=0
nina4ever 14-Oct-14 12:47pm    
i have posted my solution. thanks for your concern
ZurdoDev 1-Oct-14 16:04pm    
Since we can't see anything you have done we can only guess. It sounds like the master grid is a different control altogether and therefore the focus was not lost on the other grid so the validation did not fire.
nina4ever 2-Oct-14 3:57am    
thanks,
validation DOES FIRE successfully and e.Cancel is set to TRUE. i am sure about that. but it just doesn't stop other events from firing and doesn't prevent leaving cell in case focus is moved to master grid to a record which is not the parent of my invalid child(detail) record

1 solution

i have solved the problem using a simple workaround.

In detail grid CellValidating, when input is INVALID i do the following:
In addition to setting e.Cancel to TRUE i also display a MessageBox to inform the user. User will click OK to dismiss the message box, and that is enough to keep focus on the invalid cell in detail grid.

i hope that will help others.
 
Share this answer
 
v2

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



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