Link to home
Create AccountLog in
Avatar of George
GeorgeFlag for Canada

asked on

canceled deleting row in a datagridview still removes the row

In a windows form. VB.net 2005
a datagridview that is bound to a Binding Source Control...

If the row that I am deleting can not be deleted beacuse is refered in other table, the row is removed form the grid in anyway...

I need to show it back if the process failed

I have set the Delete Item from the Binding navigator to None

I have the code in the snipped

How can I show the row back or not remove the row in the first place?
Or what I am doing wrong ?
Or what is the right way to delete?

TIA
Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles  BindingNavigatorDeleteItem.Click
        Dim strDeletingItem As String
        strDeletingItem = Me.L_hazard_typeBindingSource.Current("hazard").ToString
        Try
            L_hazard_typeBindingSource.EndEdit()
        Catch ex As Exception
            MessageBox(ex.Message, MsgBoxStyle.Exclamation)
            Exit Sub
        End Try
        If Me.DS_MA_LookUpDataSet.HasChanges Then
            MsgBox("There are some changes pending to save" _
            & vbNewLine & "please save them before deleting", MsgBoxStyle.Exclamation)
        Else
 
            Select Case MsgBox("You will not be able to undo deletion." & vbNewLine & vbNewLine _
                        & "Are you sure that you want to delete '" & strDeletingItem & "'?" _
                            , MsgBoxStyle.Question + MsgBoxStyle.YesNo)
                Case vbYes
                    Try
                        Me.L_hazard_typeBindingSource.Remove(Me.L_hazard_typeBindingSource.Current)
                        Me.L_hazard_typeTableAdapter.Update(DS_MA_LookUpDataSet.l_hazard_type)
                    Catch ex As SqlException
                        If ex.Number = 547 Then
                            MsgBox("This item is referenced by at least one record and cannot be deleted", "CANNOT DELETE")
                        Else
                            ErrorMessageBox(ex.Message, MsgBoxStyle.Exclamation)
                        End If
                    Catch ex As Exception
                        ErrorMessageBox(ex.Message, MsgBoxStyle.Exclamation)
                    End Try
                Case vbNo
                    'do not delete
            End Select
        End If
    End Sub

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Are you detecting that error anywhere?  Do you have a RowsRemoved event handler?

Bob
Avatar of George

ASKER

Hi Bob,

When the SqlException err.number is 547 is because the relationship constraints

The row is not actually deleted from the database but removed form the grid...

I am not implementing RowsRemoved event handler
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of George

ASKER

Hi Bob,

Thank you for your answer

Using  Me.BindingSource1.CancelEdit() did not do the trick...but almost...

I tried that but still remove the row from the grid...

I checked the binding source and the row is removed after Me.BindingSource1.remove (as expected I guess)

but extending your idea to the Dataset.DataTable I tried the RejectChanges method and I got the row back...

Me.myDataset.myTable.RejectChanges()

I am almost there... I just have an exclamation icon in the record selector that I would like to change the wording or supress it

How can I change that error message?

Thank you
Avatar of George

ASKER

Thanks BOB,
About how to make disappear the error message is another question or subject to more points... ;)
Thank you very much!