I'm once again getting stuck on WinForm DataBinding. I work primarily with forms that bind to entity business objects so the thing I do a lot of is binding textboxes, checkboxes etc. to business object properties. The new BindingSource object in VS.NET makes this relatively easy.
- You drop a BindingSource object on the form
- You add a Project DataSource and point it the Entity object
- Note that the object must have properties not fields to bind to
- You then add the data mapping to the control
- Do this in DataBindings (Advanced)
- You specify the property of the control (ie. Text) to map a property of the entity (ie. cCustomerEntity.FirstName)
- This binding is a mapping to a specific type
- You specify how the control update (OnValidation, OnPropertyChange)
- When all your bindings are hooked to controls you bind the actual data source object:
this.MyBindingSource.DataSource = this.CustomerEntity;
in code.
and you start binding with the entity and controls kept in sync. All of this works well. As I mentioned in a previous entry when I was struggling <g>, the actual databinding mechanics are much nicer in .NET 2.0 where. For one the new controls manage typed values much nicer with some basic inputmasking to match the type. So numerics right align and don't allow entering non-numeric data for example.
So far so good…
Custom Control Binding - Another story
But I now have yet another problem. I have a custom wwDateTimeDisplay control, that allows editing both Date and Time in a single control on a form. It's a composite control that includes a custom DateTimePicker and a plain textbox for the time. There's a Value property which looks like this:
/// <summary>
/// The DateTime value that the control displays
/// </summary>
[Bindable(true)]
public DateTime Value
{
get {
this._Value = this.GetDate();
return _Value;
}
set {
if (value != _Value && this.ValueChanged != null)
this.ValueChanged(this,EventArgs.Empty);
_Value = value;
this.SetDate(_Value);
}
}
private DateTime _Value;
protected DateTime GetDate()
{
DateTime Date = this.txtDate.Value;
string DateString = Date.ToString("d") + " " + this.txtTime.Text.Trim();
DateTime dt = DateTime.Parse(DateString);
return dt;
}
protected void SetDate(DateTime CurrentDateTime)
{
this.txtDate.Value = CurrentDateTime.Date;
this.txtTime.Text = CurrentDateTime.ToString("t");
}
The control inherits UserControl and it works just fine to display and update date time values on a form.
But I cannot get this control to work correctly with DataBinding. If I set up DataBinding to the control using a BindingSource and using:
this.txtTimeIn.DataBindings.Add(new Binding("Value", this.bindEntry, "Timein", true));
I get inbound binding to work perfectly, but the binding never updates the underlying object (CustomerEntity.Timein).
I do see OnValidate fire in the object which should be the hook that the BindingSource is using. But the Entity Property is not updated.
I'm probably missing something obvious, but after searching around for an hour today and running into lots of small pieces I still haven't found a resource that explains how the BindingSource actually hooks up the values. Specifically what triggers the OnValidation or OnPropertyChanged operations that presumably update the control and underlying data sources.
Anybody know of a good resource?
Other Posts you might also like