471,570 Members | 1,305 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

Databinding to business object

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
Nov 30 '05 #1
1 2506
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(IsNul lable=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(pro pertyName));
}
}
}
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

"David Veeneman" <da****@nospam.com> wrote in message
news:uP**************@TK2MSFTNGP14.phx.gbl...
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

Dec 1 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Tim Jarvis | last post: by
2 posts views Thread by Steven | last post: by
2 posts views Thread by ulrich schumacher | last post: by
7 posts views Thread by Vlado Jasovic | last post: by
3 posts views Thread by nagar | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.