Friday, June 6, 2014

avoid fire selected index change event while data binding in combo box (windows application)

Rain rain go away come aging another day oh no please don’t come again. Rainy session has begun and flooded everywhere.
Today I’m going discuss about another issue in combobox (windows app)
The problem was when we’re binding combobox , if there’re combo selected index change event it will be triggered while data binding.to avoid the issue , attach and detached the event handler.
this.cmbDyeJob.SelectedIndexChanged += new System.EventHandler(this.cmbDyeJob_SelectedIndexChanged);

cmb.DataSource = lst;

this.cmbDyeJob.SelectedIndexChanged -= new System.EventHandler(this.cmbDyeJob_SelectedIndexChanged);

Solution 
Add SelectionChangeCommitted instead of SelectedIndexChanged Event.

Solution 2 (Tricky got form stackoverflow)
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!((ComboBox)sender).Focused) return;
        }
In the 2nd solution, it fires the selected index change event but it’ll return after first line is executed.(noted combobox should not be focused while binding)


Happy Coding…..

No comments:

Post a Comment