Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unbinding bindingsource

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?

like image 746
Gluip Avatar asked Jun 29 '11 15:06

Gluip


1 Answers

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

like image 170
Luc Morin Avatar answered Nov 16 '22 01:11

Luc Morin