How to Cancel Delete in BindingNavigator

Submitted by: 
Visitors have accessed this post 21608 times.

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()
  5. Try
  6. Me.Validate()
  7. Me.ProgramsBindingSource.EndEdit()
  8. Me.TableAdapterManager.UpdateAll(Me.ProgramsDataSet)
  10. MessageBox.Show("Record successfully deleted.")
  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


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();
  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

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.