none
CurrentChanged event triggered many times RRS feed

  • Question

  • Hi,

    I have a form with many controls on it, each linked to a binding source. So lets say I change the current value via a combobox, I notice that in turn the CurrentChanged event is being triggered multiple times for the corresponding binding source.

    Why might this be happening?

    Thanks,

    Barry.
    Tuesday, February 19, 2008 4:27 PM

Answers

  •  

    Hi

    I performed a test as you described using the following code, but I didn’t reproduce your problem. The code works well on my machine.

    Code Snippet

        public partial class Form6 : Form

        {

            public Form6()

            {

                InitializeComponent();

            }

            BindingSource mybindingSource = new BindingSource();

     

            private void Form6_Load(object sender, EventArgs e)

            {

                DataTable productdt = new DataTable();

                productdt.Columns.Add("id", typeof(int));

                productdt.Columns.Add("name");

                productdt.Rows.Add(1, "Product1");

                productdt.Rows.Add(2, "Product2");

                productdt.Rows.Add(3, "Product3");

                productdt.Rows.Add(4, "Product4");

                productdt.AcceptChanges();

                this.cboProduct.DisplayMember = "name";

                this.cboProduct.ValueMember = "id";

                mybindingSource.DataSource = productdt;

                this.cboProduct.DataSource = mybindingSource;

                mybindingSource.CurrentChanged += new EventHandler(mybindingSource_CurrentChanged);

            }

     

            void mybindingSource_CurrentChanged(object sender, EventArgs e)

            {

                System.Console.WriteLine("CurrentChanged");

            }

        }

     

    As the document says, there are many reasons that this event may occur.

    ·         The current position of the List changes.

    ·         The DataSource or DataMember properties change.

    ·         The membership of the underlying List changes, which causes Position to refer to a different item. Examples include adding or deleting an item before the current item, deleting or moving the current item itself, or moving an item to the current position.

    ·         The underlying list is refreshed by a new sorting or filtering operation.

    You may have some related data that may force this event trigger again. Or you may register this event more than once.

    Hope this can help you find the solution. If not, could you please provide detailed steps to reproduce this problem?

    Best regards.
    Rong-Chun Zhang

    Monday, February 25, 2008 9:05 AM