Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Bound object gets updated when datasource of ComboBox changed
 

Bound object gets updated when datasource of ComboBox changed

Hi All,

I've ran into a strange problem which I'm now trying to understand. I have an object bound to a ComboBox ("SelectedValue"). Theitems of the ComboBox arepopulated with a BindingSource whichitself is populated by a simple list. In some case I need to apply a filter to the items. Do achieve this I change the list that is bound to the BindingSource (BindingSource.DataSource = newlist). As soon as I do this my data bound object gets updates with a new value.

I think I understand why the update is happening. The SelectedIndex suddenly points to a new value therefor the object gets update. But there are some strange dependecies:

- if I use DataSourceUpdateMode.OnPropertyChanged for the binding the value gets updated
- if I use DataSourceUpdateMode.OnValidation for the binding the value doesn't get updated

I tried several ways of suspending the binding (this referring to the ComboBox):
- this.DataBinding[0].SuspendBinding
- this.BindingContext[DataBinding[0].DataSource].SuspendBinding (and several other versions of this)

But regardless of what I tried, the bound object was always changed when I uses OnPropertyChanged. Finally I found a workaround:

Dictionary<BindingMemberInfo, DataSourceUpdateMode> modes = new Dictionary<BindingMemberInfo, DataSourceUpdateMode>();
foreach ( Binding b in DataBindings )
{
modes.Add(b.BindingMemberInfo, b.DataSourceUpdateMode );
b.DataSourceUpdateMode =
DataSourceUpdateMode.Never;
}

ICodeTableEntry current = null;
if ( codeTableBindungSource.Position >= 0 )
current = (
ICodeTableEntry) codeTableBindungSource.Current;

codeTableBindungSource.DataSource = codes; // filtered list

if ( current != null )
codeTableBindungSource.Position = codeTableBindungSource.IndexOf( current );

foreach ( Binding b in DataBindings )
b.DataSourceUpdateMode = modes[b.BindingMemberInfo];

I don't like this way very much, but at least it solved my problem. But was I'm really wondering about is why none of the SuspendBindings() calls was working. Is that due to the fact that SuspendBinding only works with simple binding ?

Can anybody enlight me ?

Regards,
Wolfgang
Wollinet  Tuesday, August 25, 2009 1:36 PM

Hi,

SuspendBinding will not impact complex databinding.
More information: BindingSource.SuspendBinding Method http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.suspendbinding.aspx

Base on my understanding, you want to update the bindingsouce of combobox but don’t expect this changes the selectedvalue.

Do you use datagridviewcombobox? There is a faq tells about filtering bindingsource. The selected value must be in the datasource of combobox. Otherwise, this will cause a data error. So it add filter in cellBeginedit event and remove filter in cellEndEdit event. This will not cause a validation error.

27. How do I have a combo box column display a sub set of data based upon the value of a different combo box column?

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq27

If you bind another control with the same bindingsource of combobox control, it’s recommend to set these two controls with different bindingsource. This will avoid currencymanager to synchronize the selected items.

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Monday, August 31, 2009 2:09 PM

Anybody any thoughts on this ? I still hope there is a "nice" way ...

--
Wolfgang

Wollinet  Friday, August 28, 2009 3:22 PM

Hi,

SuspendBinding will not impact complex databinding.
More information: BindingSource.SuspendBinding Method http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.suspendbinding.aspx

Base on my understanding, you want to update the bindingsouce of combobox but don’t expect this changes the selectedvalue.

Do you use datagridviewcombobox? There is a faq tells about filtering bindingsource. The selected value must be in the datasource of combobox. Otherwise, this will cause a data error. So it add filter in cellBeginedit event and remove filter in cellEndEdit event. This will not cause a validation error.

27. How do I have a combo box column display a sub set of data based upon the value of a different combo box column?

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq27

If you bind another control with the same bindingsource of combobox control, it’s recommend to set these two controls with different bindingsource. This will avoid currencymanager to synchronize the selected items.

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Monday, August 31, 2009 2:09 PM
No, it's not a DataGridViewComboBox. So it seems I have to stick to may "workaround". At least it's working. Thx for the help anyway.

--
Wolfgang
Wollinet  Thursday, September 03, 2009 6:41 AM

You can use google to search for other answers

Custom Search

More Threads

• Binding to a custom object
• UserPreferenceChangedEventHandler
• Datagrid data entry
• How to clear an integer column with a integer parsing error
• datagridview with dataset
• how to bind when the number of parameters will vary
• help! freezing datagrid columns in c#
• DataGrid
• Edit the cell when the contextMenuItem is clicked...
• Problem with thread and dataGrid