2016-03-08 65 views
8

我有2 DTO类:无法绑定到DataSource上的属性或列“列名称”。参数名称:数据成员

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(); 
    } 
} 

我有在它结合CustomerDto绑定源的形式。我也有地址栏的自定义控件。此自定义控件具有一个绑定源,该绑定源绑定到AddressDto控件的文本框已正确绑定到地址属性。

控制公开以下属性:

[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)] 
[Browsable(false)] 
public object Address 
{ 
    get { return bindingSource.DataSource; } 
    set { bindingSource.DataSource = value; } 
} 

在一台机器,我不得到CheckBinding()任何错误。但是,在另一台机器上,当我尝试在运行时打开表单时,出现上述异常。在设计时,一切正常。

控制有3 TextBoxes和设计师增加了以下绑定:

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)); 

任何想法,问题出在哪里,可以吗?

+0

你在绑定中使用属性路径吗?如果是的话,它可能类似于这个问题[绑定和多态 - 无法绑定属性或列(Winforms)](http://stackoverflow.com/questions/33789575/binding-and-polymorphism-cannot-bind-property-or -column-winforms/33790268#33790268) –

+0

@IvanStoev号我没有使用属性路径。 –

+1

如何提供某种[mcve] –

回答

5

我改变了代码:

[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); 
     } 
    } 

价值为System.DBNull。随着上述变化,异常不再被抛出。

这解决了这个问题。然而,为什么价值是DBNull仍然不清楚,因为我使用纯POCO类作为我的绑定资源的数据源。

+0

winforms数据绑定是为了定位数据表而构建的,它绑定到对象的能力稍后会变成boltet。这就是为什么你得到一个DBNUll而不是null。 –