Hello, I have a datagrid and I want to delete selected row from that grid. It is working fine upto this. but I want to add some advance functioning with msgbox YES-No buttons. If user select some row and click on delete button, A msgbox with YES/No options appears. If user clicks YES, row should be deleted but if No is clicked then that row should not be deleted.

I have tried it many ways, but failed to achieve goal. Your help on this would be appreciable.. Thanks

Below is the code:

view source
print?

Private Sub btnDeleteSitting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteSitting.Click

MsgBox("Are you sure to DELETE?", MsgBoxStyle.Critical + MsgBoxStyle.YesNo, "WARNING")

If MsgBoxResult.Yes 

cmd.Connection = Con
Con.Open()
cmd.CommandText = "Delete from tbl_Sittings where (SittingID = N'" & lblSittingID.Text & "')"
cmd.ExecuteNonQuery()
Exit Sub
ElseIf MsgBoxResult.No Then
End If
Con.Close()
End Sub

See if this helps.
Option 1.

Select Case MsgBox("Are you sure to DELETE?", MsgBoxStyle.Critical + MsgBoxStyle.YesNo, "WARNING")
            Case MsgBoxResult.Yes
                MsgBox("YES Clicked.")
            Case MsgBoxResult.No
                MsgBox("NO Clicked.")
        End Select

Option 2.

If MsgBox("Are you sure to DELETE?", MsgBoxStyle.Critical + MsgBoxStyle.YesNo, "WARNING") = MsgBoxResult.Yes Then
            MsgBox("YES Clicked.")
        Else
            MsgBox("NO Clicked.")
        End If

Option 3.

Select Case MessageBox.Show("Are you sure to DELETE?", "WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2)
            Case DialogResult.Yes
                MsgBox("YES Clicked.")
            Case DialogResult.No
                MsgBox("NO Clicked.")
        End Select

Option 3 can also be used in a If/End If Statement and the selection of which button to be highlighted can also be changed.

I like the second one which is more easier to use and understandable.....!!!

This question has already been answered. Start a new discussion instead.