We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to prevent selectedindexchanged event when DataSource is bound

Hi,

I have a form with a combobox. On load event I call

            Data.Clear();
            Data.Merge(getdata());
            Data.AcceptChanges();

After that, value on  combobox auto set at first index.

In my code, I don't use selectedindexchanged event, but this event always fires when I update data.

Is there a way of avoiding such behaviour?

Thanks

7 Replies

AA Arulraj A Syncfusion Team November 6, 2018 04:37 PM UTC

Hi Hieudt, 

Thank you for using Syncfusion products. 

When the underlying data source is updated it will automatically fires the SelectedIndexChanged event which is a default behavior. If you need to avoid it you can unwire the event before the data source update is done and wire it again once completed. The following code explains the same. 
  
C# 
this.ComboBoxAdvBound.SelectedIndexChanged -= ComboBoxAdvBound_SelectedIndexChanged; 
this.dataSet21.Sex_Description.Clear(); 
this.dataSet21.Sex_Description.Merge(newDT); 
this.dataSet21.Sex_Description.AcceptChanges(); 
this.ComboBoxAdvBound.SelectedIndexChanged += ComboBoxAdvBound_SelectedIndexChanged; 


Let us know whether this helps also if you need any further assistance on this. 

Arulraj A 



HI hieudt November 7, 2018 04:49 AM UTC

Hi,

Thanks for your reply

I knew I can unwire that event before update data, but, is there another solution ? Likely remove that default behavior, don't automatically fire event

Thanks


KR Kannan R Syncfusion Team November 8, 2018 04:14 AM UTC

Hi Hieudt, 
 
Thank you for your update.  
 
We hope you would like to get event notification only when user changes / selects the value in ComboBoxAdv. If so, we suggest you the use SelectedChangeCommitted or handle SelectedIndexChanging event in ComboBoxAdv for this requirement.  
 
SelectedChangeCommitted event 
 
SelectionChangeCommitted is raised only when the user changes the combo box selection.  
 
SelectedIndexChanging event 
 
SelectedIndexChanging is raised before SelectedIndexChanged event and it is a CancelEventArgs type of event. So user can handle SelectedIndex modification in ComboBoxAdv, by using this event.  
 
Please find the code snippet in below. 
 
Code Snippet : [C#] 
 
 
        /// <summary> 
        /// Occurs before SelectedIndexChanged event is triggered.  
        /// </summary> 
        private void ComboBoxAdvBound_SelectedIndexChanging(object sender, SelectedIndexChangingArgs e) 
        { 
            if (e.NewIndex == -1) 
            { 
                MessageBox.Show("SelectedIndexChanging event is occurred"); 
                e.Cancel = true; 
            } 
        } 
 
        /// <summary> 
        /// Occurs when user selects the value in ComboBoxAdv 
        /// </summary> 
        private void ComboBoxAdvBound_SelectionChangeCommitted(object sender, EventArgs e) 
        { 
            MessageBox.Show("SelectionChangeCommitted is changed"); 
        } 
 
        /// <summary> 
        /// Occurs when SelectedIndex is modified in ComboBoxAdv 
        /// </summary> 
 
        private void ComboBoxAdvBound_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            MessageBox.Show("SelectedIndexChanged event is occurred"); 
        } 
 
 
 
We have prepared sample for your reference.  
 
 
 
Please try the above solution and let us know if it helps.  
 
Regards, 
Kannan 



HI hieudt November 9, 2018 09:34 AM UTC

Hi,

Thanks for your reply

I have modified your sample and sent back

I want in ComboBoxAdvBound, if I don't set value for it when bound data, it stays blank until I select value

In this sample I use generic list, but in real project, data load from sql and fill in combobox, so I can't set first record blank as default value

Thanks

Attachment: CS_12ad7b1d.rar


AA Arulraj A Syncfusion Team November 12, 2018 12:52 PM UTC

Hi Hieudt 

Thank you for the update. 

Query : but in real project, data load from sql and fill in combobox, so I can't set first record blank as default value? 
 
We have checked the reported query, that values shows empty until you selected the value from list on loading the value using SQL in ComboBoxAdvBound.  But, we could found that the values has been updated correctly at load time and it is not blank. Please find the video for the same. 


Can you please check this and let us know whether we are following correct procedure to reproduce the issue. If not, kindly let us know about the details and also your currently using product version details, it would be helpful for us to proceed on this further. 

Arulraj A 



HI hieudt November 13, 2018 04:27 AM UTC

Hi,

Thanks for your reply

The sample you gave me is not what I desire

I have a combobox, on form load event, I load data from SQL to combobox, after that value on combobox auto set at first index, because SelectedIndexChanged event auto fired, whether I call it or not, I do not want that behavior, I want it stays blank until I actually change its value

At previous post you suggested me unwire SelectedIndexChanged event before load data, but is there another solution ?

I am using version 15.1.0.41

Thanks


AA Arulraj A Syncfusion Team November 15, 2018 04:33 PM UTC

Hi Hieudt, 

Sorry for the inconvenience caused. 

We are able to see the same behavior in the Microsoft ComboBox when loading data from SQL, being a default behavior we recommend you to change the behavior by unwire the event before the data source update and once done wire it again. 

Please let us know if you require further assistance in this. 

Arulraj A 


Loader.
Live Chat Icon For mobile
Up arrow icon