I have 2 DTO classes:
public class AddressDto { public string Street { get; set; } public string City { get; set; } public string PostCode { get: set: } } public class CustomerDto { public int Number{ get; set; } public string Name { get; set; } public AddressDto Address { get: set: } public CustomerDto() { Address = new AddressDto(); } } I have a form with a binding source in it that binds to CustomerDto. I also have a custom control with the address fields. This custom control has a binding source that binds to AddressDto The control's textboxes are correctly bound to the address properties.
The control exposes the following property:
[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)] [Browsable(false)] public object Address { get { return bindingSource.DataSource; } set { bindingSource.DataSource = value; } } On one machine I do not get any errors on CheckBinding(). However on another machine I get the above exception when I try to open the form in runtime. In designtime everything works fine.
The control has 3 TextBoxes and the designer adds the following bindings:
this.bindingSource.AllowNew = true; this.bindingSource.DataSource = typeof(AddressDto); this.txtStreet.DataBindings.Add(new Binding("Text", this.bindingSource, "Street", true)); this.txtCity.DataBindings.Add(new Binding("Text", this.bindingSource, "City", true)); this.txtPostCode.DataBindings.Add(new Binding("Text", this.bindingSource, "PostCode", true)); Any ideas where the problem can be?
2 Answers
Answers 1
I changed the code to:
[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)] [Browsable(false)] public object Address { get { return bindingSource.DataSource; } set { if (value != null && value != System.DBNull.Value) bindingSource.DataSource = value; else bindingSource.DataSource = typeof(AddressDto); } } Value was System.DBNull. With the above change, the exception is not thrown anymore.
This solves the problem. However, why value is DBNull is still not clear because I'm using pure POCO classes as my datasources for my bindingsources.
Answers 2
The winforms databinding was built to target datatables, its ability to bind to objects was boltet on later.
That why you get a DBNUll instead of a null.
0 comments:
Post a Comment