Databinding to business object

  • Thread starter Thread starter David Veeneman
  • Start date Start date
D

David Veeneman

Hi-- I'm trying to databind a business object to several controls using the
DataBindings property of the controls, like this:

textBoxStartDate.DataBindings.Add("Text", CurrentStep,
"StartDate",
true, DataSourceUpdateMode.OnValidation, String.Empty,
"d");

Where CurrentStep is a business object of type ProjectStep, with several
properties, such as the StartDate property shown above.

When the form initializes, and a ProjectStep object is plugged into
CurrentStep, the controls update their values as expected. But if
currentStep is set to another ProjectStep object after that, the controls
don't update. They retain the values from the first ProjectStep object
passed in.

What do I need to do to make the controls update when the CurrentStep object
is updated? Is there a call I need to make when the call is updated? Thanks
in advance for your help.

David Veeneman
Foresight Systems
 
If you are changing the actual object (i.e. a new object), then your
approach won't work (short of manually resetting all of your bindings). If
you are simply updating values on the *existing* object (using setters),
then your object should implement the INotifyPropertyChanged interface, and
invoke the PropertyChanged event in every setter. As an example (taken
indirectly from WSDL.exe output):

public class MyObj : object,
System.ComponentModel.INotifyPropertyChanged{
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public System.Nullable<System.DateTime> ImplementationActual {
get {
return this.implementationActualField;
}
set {
if ((this.implementationActualField != value)) {
this.implementationActualField = value;
this.RaisePropertyChanged("ImplementationActual");
}
}
}
public event System.ComponentModel.PropertyChangedEventHandler
PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler
propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new
System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}


The reason the former doesn't work is that your controls are still quite
happily bound to the existing object; the way to do this is to not bind
directly to the object, but to a BindingSource, e.g.

1: place a binding source onto your form (as a component) and link it to the
right data type
2: setup your bindings (example below where implementationActual is a
date-picker):

this.implementationActual.DataBindings.Add(new
System.Windows.Forms.Binding("Value", bindingSource, "ImplementationActual",
true));

3: add the object to the BindingSource:

List<MyObj> data = new List<MyObj>();
data.Add(item);
bindingSource.DataSource = data;

Now when you change the object in the data source (either by resetting the
list to a new instance, or by removing, adding, etc to the data-source and
moving to the correct record) your bindings update themselves correctly.

Does this help?

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top