Validation DataGridView Windows Forms

315 views Asked by At

I have two forms with grid views, one made from a database data source and one from entity framework dbset's. The EF one, when I press add row it adds a row and I can edit the ID. The other form, when I add row the ID is negative. Rather than incremented from the previous. Not sure why. But the issue I'm having is that when I click out of a column I added without filling it all in it validates and gives an error. I want this to happen at the end when save is clicked.

Right now I have these event handlers for the tables:

AddNewItem_Click, SaveItem_Click, DeleteItem_Click, CellContentClick, and more importantly: CellValidating, CellValidated, CellEndEdit, and most importantly CellValueChanged.

I read somewhere that using CellValueChanged is the best way to assure that the validation does not occur when I change cells. But I can't find an example. Basically, as far as this website validated says about validating and validated, there are steps that occur when you change cell with mouse, I don't care about keyboard.

  1. Enter 2. GotFocus 3. LostFocus 4. Leave 5. Validating 6. Validated

When the cell loses focus, I get the error because it failed the validating. However on my EF created form, this does not occur. How can I stop this behavior from validating every time a cell loses focus?

My other question is this: as I said before in my non-EF form with the DataGridViews, when I add a row it adds a negative number as the ID which is the PK. I also can't edit that cell by clicking in it. In the EF form, I can click in the cell and edit it, and it doesn't add a negative number and while it may not make the numbers consecutive I can edit them myself and fix them. Why am I having these difference between the two? The only difference is one's datagridviews are created from EF data source and the others are created from a raw sql database data source.

So how can I change cell without validating, and how can I get these ID's to not be negative? Also how can I make the cells in ID column editable like in the EF form.

Any advice would help a lot, and examples help me a ton as I'm a visual learner.

If you need any code or information please ask and I'll post it, I'm not sure what to post as there hasn't been customization in the event handlers.

Thanks

1

There are 1 answers

0
ss7 On BEST ANSWER

The answer is to add this.CausesValidation = false to the event handlers that are being triggered.