Stop Row Deleting

I have a DataGridView with some rows displayed and would like to check if there is any related data before deleting the row.

I have a foreign key constraint but would like to popup a nice message when the user tries to delete the row rather than wait until they click on save and have the ugly, default error message.

My first thought were to put some code in the RowDeleting routine. I have tried putting in a popup message along with e.row.canceledit and e.row.rejectchanges but the row still gets 'deleted' (until I click on save and get the FK error)

Any suggestions



Answer this question

Stop Row Deleting

  • Robin H. Sanner

    Do you mean the delete button on the bindingnavigator

  • Mickey777

    hi,

    you can depend on the message box result to proceed  deleting or not like for example

    Private Sub btnDeleteCat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteCat.Click

            Dim Result As DialogResult = MessageBox.Show("Are you usre you want to Delete Catigory " & vbNewLine & "Delete AnyWay ", "Warnning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, _
            MessageBoxDefaultButton.Button2)

            If Result = Windows.Forms.DialogResult.Yes Then

                Try

    ' first call other sub to delete all related records to this record

                    Call DeleteSubCats(CatigoryID)

    'delete this record

                    Dim CatRow As DataSet1._catRow
                    CatRow = MyDataSet._cat.FindBycatid(CatigoryID)
                    CatRow.Delete()

                Catch ex As Exception
                    MessageBox.Show("There is an error occured during deletation" & ex.Message)
                End Try

            End If
        End Sub


        Private Sub DeleteSubCats(ByVal CatigoryID As Integer)
            Try

                'get list of subcatrows depending on the topic id
                Dim SubCatRows() As DataSet1._subcatRow
                SubCatRows = MyDataSet._subcat.Select("subcatcatid ='" & CatigoryID.ToString & "'")
                Dim SubCatSingleRow As DataSet1._subcatRow
                For Each SubCatSingleRow In SubCatRows

                    SubCatSingleRow.Delete()
                Next

            Catch ex As Exception
                Throw ex
            End Try

        End Sub

    hope this helps



  • nightchaser

    Thanks Ken,

    Although this works I can still delete the row by clicking the Delete button so I guess I will have to put a similar piece of code into DeleteItem_Click, any idea how to cancel the delete here as e.cancel = true does not work


  • dityaprirody

    Thanks Ken,

    I will now just put my checks into a function that returns true/false to say if the delete is allowed and I can call this from each of the two events, cheers


  • Wa5h

  • SlashSlash

    Thanks

    Yes, I realise that I could do this with a button and manually fire the deletion but this would not then fire if the user simply pressed the delete key on the selected row in the DataGridView


  • RadRad

    Handle the datagridview's UserDeletingRow event.  Setting e.cancel to true will stop the delete.

     

        Private Sub DataGridView1_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles DataGridView1.UserDeletingRow
            If MessageBox.Show("Are you sure ", "Delete Row", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
                e.Cancel = True
            End If
        End Sub



  • glenmaclarty

    Yes, the automatically created delete button on the bindingnavigator - I may choose not to use this at all yet but incase I do I would need to know how to cancel this method aswell


  • Stop Row Deleting