Datagridview add new row

thomas008

Well-known member
Joined
Feb 17, 2009
Messages
54
Location
Belgium
Programming Experience
Beginner
Hi

I have an application that has a datasource bound to a datagridview.
All of my validation works fine. Only when i press the plus sign of the bindingnavigator twice my program crashes. Now my question is how can i make sure that the user has to correctly fill in an added row before he can click to add another?
 
This should give you a place to start: How Do I: Add Validation?

I used this tutorial as a basis for my validation. All my data gets validated and i've disabled the save option when the data is invalid. But when i add a row and don't enter any values and then add a new row my program crashes because a NoNullAllowedException is thrown. I don't know how to catch this exception or prevent it from happening. Normaly users won't add a row without filling in values. But i just don't want my program to crash!
 
Also when I debug and click the add button for the first time i see the datable_TableNewRow event is raised however the second time i click this does not get raised. It seems the second time no event gets raised not even the datagrids RowValidating event.

Now i can not set default values in my columns because the values are not predictable. Does anyone know how to fix this?
 
You could try changing the AddNewItem property for your binding navigator to (none) and then only call add new if your DataSet doesn't have errors.

BindingNavigatorAddNewItem.Click

VB.NET:
		Me.YourBindingSource.EndEdit()
		If Not Me.YourDataSet.HasErrors Then
			Me.YourBindingSource.AddNew()
		End If
 
To follow MattP's advice you'll need to perform the AddNew yourself; you'll need to unlink the BN from the BS.AddNew by removing the relevant property denoting that button as the add new button

Note that calling EndEdit might raise the exception, as it attempts to commit the row-with-blank-values into the datatable
 
Well i found a way to work around it. I dropped the constraints by setting the EnforceConstraints property of my dataset to false. If i make sure my primairy keys are unique i think i shoudn't have a problem updating to the database or should i set EnforceConstraints to true before i update so I can see if there are any errors?
 
Note that calling EndEdit might raise the exception, as it attempts to commit the row-with-blank-values into the datatable

I should have mentioned that this was the case. When testing this I put that code in a Try/Catch block and specifically looked for a NoNullAllowedException.
 

Latest posts

Back
Top