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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to prevent selectedindexchanged event when DataSource is bound

Thread ID:

Created:

Updated:

Platform:

Replies:

140762 Nov 6,2018 07:05 AM UTC Nov 15,2018 04:33 PM UTC WinForms 7
loading
Tags: ComboBoxAdv
hieudt
Asked On November 6, 2018 07:05 AM UTC

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

Arulraj A [Syncfusion]
Replied On 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 


hieudt
Replied On 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

Kannan R [Syncfusion]
Replied On 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 


hieudt
Replied On 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

Arulraj A [Syncfusion]
Replied On 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 


hieudt
Replied On 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

Arulraj A [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

or the page will be automatically redirected to sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;