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