How to Cancel Delete in BindingNavigator

When you add a DataSet to your Windows Form a BindingNavigator is automatically created with navigation to your record and it also includes Delete and Add button. When you click the Delete button there is no confirmation if you want to cancel the operation. The solution to this is to set the DeleteItem to None. See screenshot below: Here's the code in the Delete button under BindingNavigator:
  1. Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  2. If MessageBox.Show("Are you sure you want to delete this record?", "Delete record", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then
  3. Me.ProgramsBindingSource.RemoveCurrent()
  4.  
  5. Try
  6. Me.Validate()
  7. Me.ProgramsBindingSource.EndEdit()
  8. Me.TableAdapterManager.UpdateAll(Me.ProgramsDataSet)
  9.  
  10. MessageBox.Show("Record successfully deleted.")
  11.  
  12. Me.Close()
  13. Catch ex As Exception
  14. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  15. End Try
  16. End If
  17. End Sub

Comments

Submitted byFrank J. Perricone (not verified)on Mon, 05/04/2015 - 21:54

Here's some corresponding C# code:
  1. private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
  2. {
  3. if (MessageBox.Show(
  4. "Are you sure you want to delete this record?",
  5. "Delete Record", MessageBoxButtons.YesNo) == DialogResult.Yes)
  6. {
  7. regionsBindingSource.RemoveCurrent();
  8.  
  9. try {
  10. this.Validate();
  11. regionsBindingSource.EndEdit();
  12. tableAdapterManager.UpdateAll(aFGDevlDataSet);
  13. MessageBox.Show("The recordhas been deleted.");
  14. } catch (Exception ex) {
  15. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  16. }
  17. }
  18. }
I simply encoded the names of the specific binding source and data set because I wasn't sure how to make it independent like you did -- the "this" construct doesn't seem to have them, or if it does, I didn't immediately see where and didn't have time to dig.

Add new comment