Question How do I prevent the implied INSERT from executing from a databound Datagridview?

robertb_NZ

Well-known member
Joined
May 11, 2010
Messages
146
Location
Auckland, New Zealand
Programming Experience
10+
I am a relative newbie with VB, having spend more time working with web pages using ASP.NET. I use VS2008.

In my database I had defined a table called "Questions", and I have a windows program with a datagridview (named "gdvQuestions") that displays a single text field from this table. Following my normal practice I have defined a tableadaptor, table, and tablerow called "taQuestions", "tbQuestions", and "trQuestions" respectively, and bound gdvQuestions to this. So far so good, everything works perfectly. However, if you make changes to the gridview, while these are reflected in the tableadaptor (as shown by other gridviews and controls on the form), they were not being updated back to the database. Edits were ignored (as far as the database was concerned), and inserts were causing an error.

I solved the problem for edits with the following code that performs an explicit update. For an existing row, the record key is column 0 (hidden): -

Private Sub gdvQuestions_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gdvQuestions.CellEndEdit
Dim dgv As DataGridView = DirectCast(sender, DataGridView)
If e.RowIndex >= tbQuestions.Count Then ' New row inserted by entering into the bottom (insert) row of the grid
AddQuestion(dgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString)
Else
NTQid = dgv.Rows(e.RowIndex).Cells(0).Value
GetQuestion(NTQid)
trQuestions.NTQText = dgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString
taQuestions.Update(trQuestions)
End If

End Sub

If the row exists already we take the Else branch, update the tablerow field within trQuestions, and then update the database with taQuestions.update(trquestions). Works perfectly.

However for new records, after correctly adding the new record to the database through AddQuestion, the program then fails on exiting the event handler, with a message: -
"The following exception occurred in the DataGridView:

System.Data.NoNullAllowedException: Column 'NTQid' does not permit nulls.
at ......

The issue is that after the Insert has been done successfully within AddQuestion, the system then attempts to do the default Insert that is within the bound datasource. It can't do this because it doesn't have enough data, so of course it fails.

What I want to do is to tell the system not to bother: I've already done the Insert. But how do I do this? I looked on the web where it seemed that I should execute "AcceptChanges". The only object that has this method is the table, tbQuestions, there is no AcceptChanges method or property with gdvQuestions, taQuestions, or trQuestions. However if I add: -
taQuestions.AcceptChanges()
into this event handler, the only effect is to cause the error message to appear on this statement instead of after the END statement. taQuestions.RejectChanges() is equally useless.

I wondered about "e.cancel", but in this situation e doesn't have once.

So what do I do next?

Thank you, Robert.
 
Hi Robert - I'm new, but I have a similar issue - I have a simple form with a bound listview to selectreferences , bound fields to edit records, plus a navigating strip at the top for add, delete, save plus record navigation. If I press the + icon to insert the record, add the details etc , it works fine. However, if i press the + icon to insert the record, and then click on any other icon, or the listview, it tries to insert a null record, and I receive the error message that you are receiving - and i can not find which event is triggering the insert - so anyone with experience on databound controls, please help
 
In the meantime I've bypassed the issue by setting up gdvquestions to disallow the insert row, and providing a separate [New Record] button. However this is a crude solution, I'd prefer an answer to this problem as this would allow me to program a more elegant solution.
 
Back
Top