Hi all.

I am using a DataGridView bound to a BindingSource that has its DataSource property set to a DataTable within a DataSet in the project. Some of the columns within the DataTable do not alllow null. If the user is on a new row and they do not enter a value for one of these columns they will get a NoNullAllowedException when they try to leave the row and then the row dissappears. I am trying to handle this error and stop the row being lost.

So I have added an event handler to the DataGridView.RowValidating event to try and deal with this and I check the relevant columns all have a value within this event. If one or more do not have a value I set the errortext for the row and set the events e.Cancel to true so the focus does not leave the row. The problem is that I have a Save button on the form and if the user tries to leave the grid by clicking this button the program goes off in some sort of infinite loop right after the RowValidating event. It never actually reaches the code behind the button, and I have found that if I simply put e.Cancel = true in the RowValidating event and no other code I get the same issue. If I click out of the grid anywhere else but on the button everything works fine. I have seen a couple of similar posts in my search for an answer, but no solution. It seems to be so easy to recreate I think there must be a known issue with this somewhere Anyone have any ideas


Re: Windows Forms Data Controls and Databinding DataGridView RowValidating event - infinite loop

Ken Tucker

I usually assign a value to a cell that does not allow nulls in the DataGridView's DefaultValuesNeeded event. In some cases I just set the cell value to a space to prevent the error. You can also handle the DataGridView's DataError event to keep the error from showing.

Re: Windows Forms Data Controls and Databinding DataGridView RowValidating event - infinite loop


Hi Ken.

There are many case where we don't want a default value to be automatically entered by the application, so the DefaultValuesNeeded event is not going to work in these cases. I am able to hide the error through the DataError event, but this does not prevent the original problem of new rows being removed if you leave the row and hit an error. Interestingly, this event also has an e.Cancel option, and if I set it to true I get the same infinite loop problem. I have just found also that the button issue is a red herring, as clicking in a text box also leads to the problem.

Re: Windows Forms Data Controls and Databinding DataGridView RowValidating event - infinite loop


This gets weirder by the minute. I have now linked this issue to the form being an MDI child. Steps to recreate are as follows:-

Create a form, form1, and set its MDIContainer property to true

Create another form, form2, and add a button to form1 that creates an instance of form2 and sets its MDIParent property to form1

Create a DataSet and add a DataTable to it

Add a single column to the DataTable and set its AllowDBNull property to false

Add a DataGridView to form2 and link it to the DataTable via a BindingSource component and an instance of the DataTable dragged from the Toolbox

Add a button or some other control that you can click to take focus away from the grid

Finally add a RowValidating handler to the DataGridView and add the following code

if (dgv[0, e.RowIndex].Value == DBNull.Value)


dgv.Rows[e.RowIndex].ErrorText = "You must enter a value for this field!";

e.Cancel = true;


Run the application

Click in the cell and press a key so that the row goes into edit mode

Now press backspace to delete whatever you typed so the cell is empty (DBNull)

Try to click in the textbox...... CPU jumps to 100 % and you have to kill the application from Task Manager

If you stop form2 from being an MDI child the problem stops occuring and the validation works as expected.

What the