views:

43

answers:

1

The project I came into manages databinding like this:

  1. Default object is loaded on form load.
  2. Object is databound to each control property by property in code.
  3. User selects a different object to view.
  4. All controls have their databindings cleared.
  5. All controls have a databinding added referencing the new object instead of the old.

I have seen some places in the code where previous developers used BindingManagerBase, but was unsure of the reason for it.

During my research it seems it is used to simplify databinding in the manner of PropertyManager but I can't figure out how to update it's "Current" property. Can it reduce the steps above to the following?

  1. Default object is loaded.
  2. Databindings added, and BindManager retrieved.
  3. User selects a different object to view.
  4. BindManager's reference of databount object is updated, propogating to all controls.

If not, is there an class capable of doing this?

Also is there any way that this can be done automatically with some property on the controls themselves.

Simply instantiate some hypothetical binding manager that looks for controls with a property set, and databinds them to an object specified, thus removing the need to programmatically databind each control to begin with?

+1  A: 

If this is Windows Forms, then I would recommend using a BindingSource - then you only have to set the databindings once and update the BindingSource's DataSource property when you load a new object.

(BindingManagerBase is something else - it's for when you have two controls bound to the same data source.)

Your steps would be:

  1. Create BindingSource
  2. Assign an empty object
  3. Create DataBindings between the controls to the BindingSource
  4. Assign objects to the BindingSource's DataSource property as required.
Stuart Dunkeld