Question Reference error, Delete, Datagridview

dgp

New member
Joined
Feb 2, 2010
Messages
1
Programming Experience
1-3
Hi,

I have two tables with referential integrity defined in the sql-database.
parent: projects
child: projectlines

I do not want projects to be deleted if projectlines exist.
The projects are displayed in a datagridview.

Here's part of the code:
Dim dbs As New databasedata.databasedata
Dim connstring As String = dbs.DatabaseConnection
Private dc As New DB2Linq.db2linqDataContext(connstring)

ProjectenBindingSource.DataSource = dc.projectens

Dim projectFiltered = From cust In dc.projectens _
Where cust.afgesloten = False _
Select cust
DataGridView1.DataSource = projectFiltered


'To directly write changes to the database after a change in the data, I added the following, however this doesn't fire for deletes:

Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
If Me.DataGridView1.CurrentRow IsNot Nothing Then
Try
dc.SubmitChanges()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub

I can delete a project with references. It disappears from the grid. When I commit the changes, the reference error occurs. I now how to trap it, but then it is too late, the row disappeared from the grid.

Now, if this occurs, I want to "undo" the delete in the grid, so the line shows up again, and popup a message. Or, not let the line be removed from the grid at all.

What is the right way to handle this? Should I write code to check if projectlines exist? Can I catch some event? If the dataerror event fires, the line is not in the grid anymore.
 
Back
Top