Winforms datagridview rowvalidating

My problem is that when I click into it and then I change my mind and want to cancel edit of the current row these validating methods won't let me do that if it's the first row, it seems to work when there is already some valid rows, but when it's the first it won't let me change the focus to something else.So my question is how can I have both validation and the ability to cancel edit of the datagridview row?If the value of a cell is not valid I'd like to warn the user with a message box and pressing This issue is explained in the following MSDN forums post: Data Grid View Row Validating = Index 4 does not have a value?Basically it appears to be a bug (or at least very unexpected behaviour) in the way the Data Grid View handles validation - the Message Box.Canceling this event prevents the user from exiting the cell until a valid value is entered or the edit process is canceled (via Esc key).The Data Bound Item will be updated only if the cell is validated.I'd like to obtain something similar to MS Mangement Studio: if the mandatory cell in the current row is not filled i could not add another row. const int Mandatory Column Index = 1; public Form1() private void data Grid View1_Row Validating(object sender, Data Grid View Cell Cancel Event Args e) private void data Grid View1_Cell Validating(object sender, Data Grid View Cell Validating Event Args e) I'm trying your piece of code, but there's something wrong: if I edit the mandatory cell it works, but if I only fill the other cells, leaving the mandatory empty, I can add all the rows that I want. another case: if i delete cell's value instead of inserting an invalid value, e.

") End Sub End Module I can't tell you why this happens for you but I just did a test on one of our non-modal forms and the Row Validating event does fire when we close it. With a non-modal form the Row Validating does fire, but with a modal form it doesn't. Just found an answer: posted this question on a few sites so any change can be seen here or there.

When the validation fails, the editor is closed, but the user cannot change the current row in the grid until a valid cell value is entered.

In this situation the editor is not kept active when the validation fails and pressing the Esc key does not revert the changes as the editor is already closed.

I don't mind if the current row in the datagridview is lost when edit is canceled.

EDIT: here is my validating methods: Here is how it looks when it happens, if I hit Escape key, the error message disappears, but when I try to click something else, Row Validation happens again and I back to this state: EDIT2: I use datatable as datasource for this datagridview.

Leave a Reply