BindingNavigator events

Slartibartfast 0 Reputation points
2024-11-09T02:32:53.0166667+00:00

Hi

Relatively new to C#.

I have added a BindingNavigator in code using:

		private readonly BindingNavigator BN = new(true);

Everything works fine and all the common BindingNavigator controls function correctly.

I added it in code, and set a few Properties then adding to the Form control collection.

Now I need to access one of those common controls - the 'delete item' one - so I can add code to verify the user really want to delete the record.

So, how do I find the event names and signatures so I can add a handler?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,027 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. KOZ6.0 6,510 Reputation points
    2024-11-09T06:12:55.93+00:00

    How about cloning the DeleteButton?

    using System;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    
    public partial class Form1 : Form
    {
        private readonly DataGridView dataGridView;
        private readonly BindingSource bindingSource;
        private readonly BindingNavigator bindingNavigator;
        private readonly DataTable dataTable;
    
        public Form1() {
            InitializeComponent();
    
            dataTable = new DataTable();
            dataTable.Columns.Add("ID", typeof(int));
            dataTable.Columns.Add("Name", typeof(string));
    
            dataTable.Rows.Add(1, "Alice");
            dataTable.Rows.Add(2, "Bob");
            dataTable.Rows.Add(3, "Charlie");
    
            bindingSource = new BindingSource {
                DataSource = dataTable
            };
    
            dataGridView = new DataGridView {
                Dock = DockStyle.Fill,
                DataSource = bindingSource
            };
    
            bindingNavigator = new BindingNavigator(true) {
                Dock = DockStyle.Top,
                BindingSource = bindingSource
            };
    
            using (ToolStripButton deleteButton = (ToolStripButton)bindingNavigator.DeleteItem) {
                bindingNavigator.Items.Remove(deleteButton);
                ToolStripButton newDeleteItem = CloneToolStripButton(deleteButton);
                newDeleteItem.Click += DeleteButton_Click;
                bindingNavigator.Items.Add(newDeleteItem);
            }
    
            Controls.Add(dataGridView);
            Controls.Add(bindingNavigator);
        }
    
        static ToolStripButton CloneToolStripButton(ToolStripButton oldButton) {
            var newButton = new ToolStripButton {
                Name = oldButton.Name,
                Text = oldButton.Text,
                Image = new Bitmap(oldButton.Image),
                RightToLeftAutoMirrorImage = oldButton.RightToLeftAutoMirrorImage,
                DisplayStyle = oldButton.DisplayStyle
            };
            return newButton;
        }
    
        private void DeleteButton_Click(object sender, EventArgs e) {
            if (bindingNavigator.Validate()) {
                var result = MessageBox.Show("Do you want to delete it?",
                                             "Question",
                                             MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes) {
                    bindingSource.RemoveCurrent();
                    bindingNavigator.Refresh();
                }
            }
        }
    }
    

  2. Karen Payne MVP 35,441 Reputation points
    2024-11-11T02:59:44.28+00:00

    Check out my BindingNavigator repository which demonstrates how to prompt yes/no before removing a record. A custom dialog is used that set the default button to no.

    A1


  3. Jiale Xue - MSFT 46,556 Reputation points Microsoft Vendor
    2024-11-12T06:29:20.7166667+00:00

    Hi @Slartibartfast , Welcome to Microsoft Q&A,

    To summarize this case:

    OP finally used the following code:

    Add a handler for BindingNavigator in the Form Load event

    			BN.ItemClicked += DeleteItem_Click;
    

    Code:

    		private void DeleteItem_Click(object? sender, ToolStripItemClickedEventArgs e)
    		{
    			BN.DeleteItem = null;
    
    			string? s = e.ClickedItem?.Text;
    
    			if (string.IsNullOrEmpty(s))
    			{
    				return;
    			}
    
    			switch (s)
    			{
    				case "Delete":
    					DialogResult result = MessageBox.Show("Are you sure you want to delete this item?", "Confirm Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    
    					if (result == DialogResult.Yes)
    					{
    						DeleteCurrentItem();
    					}
    					break;
    
    					// Add other button clicks here, e.g.
    					// case "MoveNext":
    					// case "MoveLast":
    					// case "MoveFirst":
    
    			}
    		}
    
    		private void DeleteCurrentItem()
    		{
    			// Check if there is a current item to delete
    			if (BS.Current != null)
    			{
    				BS.RemoveCurrent();
    			}
    		}
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.