Answered bindingsource currentchanged event

  • Monday, June 30, 2008 6:13 PM
     
     
    I have this weird behavior (maybe it is normal).

    I have a list of entities as a datasource to my bindingSource.

    I subscribe to the bindingsource.currentchanged event and the event is fired multiple times at the beginning. It seems that the number of times it is fire = to the number of entities in my list.

    Is there a logic explanation for this?

Answers

  • Friday, July 04, 2008 12:24 PM
     
     Answered

     

    Hi GoDaddy,

    As far as I know the Fill Method of a DataAdapter use the DataTable.BeginLoadData and DataTable.EndLoadData internal to prevent notifications, index maintenance, and constraints while loading data. It is the reason why the CurrentChanged happened twice.

    Code Snippet

                BindingSource bs = new BindingSource();

                DataTable dt = new DataTable();

                dt.Columns.Add("cola");

                dt.Columns.Add("colb");

     

                bs.CurrentChanged += new EventHandler(bs_CurrentChanged);

                bs.DataSource = dt;

                this.dataGridView1.DataSource = bs;

     

                dt.BeginLoadData();

                for (int i = 0; i < 20; i++)

                {

                    dt.Rows.Add("aa" + i, "bb" + i);

                    dt.AcceptChanges();

                }

                dt.EndLoadData();

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

All Replies

  • Thursday, July 03, 2008 12:20 PM
     
     

     

    Hi GoDaddy,

    I cannot reproduce the problem according to your description. Here is my testing code, when i move through the DataGridView, the CurrentChanged event only triggered one time. 

    Code Snippet

        public partial class Form24 : Form

        {

            public Form24()

            {

                InitializeComponent();

            }

     

            BindingSource bs = new BindingSource();

     

            private void Form24_Load(object sender, EventArgs e)

            {

                BindingList<MyObject> list = new BindingList<MyObject>();

                for (int i = 0; i < 20; i++)

                {

                    MyObject emp = new MyObject();

                    emp.Name = "Name" + i;

                    emp.Address = "Address" + i;

                    emp.RowState = i;

                    list.Add(emp);

                }

                bs.DataSource = list;

                this.dataGridView1.DataSource = bs;

                bs.CurrentChanged += new EventHandler(bs_CurrentChanged);

            }

           

            void bs_CurrentChanged(object sender, EventArgs e)

            {

                MessageBox.Show("CurrentChanged");

            }

        }

     

        public class MyObject

        {

            private string sName;

            private string sAddress;

            private int iRowState;

     

            public string Name

            {

                get { return sName; }

                set { sName = value; }

                 

            }

            public string Address

            {

                get { return sAddress; }

                set { sAddress = value; }

            }

            public int RowState

            {

                get { return iRowState; }

                set { iRowState = value; }

            }

        }

     

     

    But as the CurrentChanged event may happened when 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 Current property may move the possible through the entire list.

    Let me know if this helps, if not, could you please provide detailed steps to reproduce the problem?

    Best regards.
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

  • Thursday, July 03, 2008 5:40 PM
     
     
    Hi,

    It's not happening during the change in the datagridview. But at the very BEGINNING, ie after the OnLoad, the currentchanged is raised multiple times.

    Try to put a breakpoint in your handler and also subscribe to the event before setting the datasource.


    Here is my example using a dataset, the event is fired twice in my case ... dunno why. And I subscribe to the event in the designer, so it's in the InitializeComponent.

    It fires immediatly after the Fill

    Code Snippet
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    this.table_1TableAdapter.Fill(this.testDataSet.Table_1);
    }

    private void bindingSource1_CurrentChanged(object sender, EventArgs e)
    {

    }




  • Thursday, July 03, 2008 5:43 PM
     
     
    Oh maybe I should subscribe only after the datasource has been set.

    .... looks like it works .... thanks!
  • Thursday, July 03, 2008 8:05 PM
     
     
    But I'm still curious as to why subscribing to the CurrentChanged event before setting the datasource will raise the event multiple times at the beginning
  • Friday, July 04, 2008 12:24 PM
     
     Answered

     

    Hi GoDaddy,

    As far as I know the Fill Method of a DataAdapter use the DataTable.BeginLoadData and DataTable.EndLoadData internal to prevent notifications, index maintenance, and constraints while loading data. It is the reason why the CurrentChanged happened twice.

    Code Snippet

                BindingSource bs = new BindingSource();

                DataTable dt = new DataTable();

                dt.Columns.Add("cola");

                dt.Columns.Add("colb");

     

                bs.CurrentChanged += new EventHandler(bs_CurrentChanged);

                bs.DataSource = dt;

                this.dataGridView1.DataSource = bs;

     

                dt.BeginLoadData();

                for (int i = 0; i < 20; i++)

                {

                    dt.Rows.Add("aa" + i, "bb" + i);

                    dt.AcceptChanges();

                }

                dt.EndLoadData();

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs