I am using a bindingsource in my windows forms application to populate some textboxes etc in my view. Binding works OK but how can I unsubscribe my bindingSource from from my object?
bindingSource.DataSource = new Foo();//OK
bindingSource.DataSource = null;//Not ok
If I try to unbind by setting data = null
I get an exception:
System.ArgumentException : Cannot bind to the property or column Bar on the DataSource. Parameter name: dataMember
I don't want to remove all bindings to my controls (i have a lot) but would like to suspend binding as long as the bindingSource has no data....
I found a workaround like this bindingSource.DataSource = typeof(Foo);
but is this THE way?
The typeof
"workaround" is actually what the windows forms designer does when you set the BindingSource's DataSource in the PropertyGrid, and select a type from "Project data sources".
Look at the generated code in the *.designer.cs file for your form.
We use this "trick" in one of our products, and it has worked well for many years now.
Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With