Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法绑定到属性或列";列名";在数据源上。参数名称:dataMember_C#_Winforms_Data Binding - Fatal编程技术网

C# 无法绑定到属性或列";列名";在数据源上。参数名称:dataMember

C# 无法绑定到属性或列";列名";在数据源上。参数名称:dataMember,c#,winforms,data-binding,C#,Winforms,Data Binding,我有两个DTO课程: 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; s

我有两个DTO课程:

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
的绑定源。我还有一个带有地址字段的自定义控件。此自定义控件有一个绑定到
addressd的绑定源,以使控件的文本框正确绑定到地址属性

控件公开以下属性:

[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
[Browsable(false)]
public object Address
{
    get { return bindingSource.DataSource; }
    set { bindingSource.DataSource = value; }
}
在一台机器上,
CheckBinding()
上没有任何错误。然而,在另一台机器上,当我试图在运行时打开表单时,会出现上述异常。在设计时间内,一切正常

控件有3个
文本框
,设计器添加以下绑定:

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));
你知道问题出在哪里吗?

我把代码改成:

    [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类作为绑定源的数据源。

在绑定中使用属性路径吗?如果是,它可能类似于这个问题@IvanStoev No。我没有使用属性路径。提供某种类型的winforms数据绑定如何?winforms数据绑定是为目标数据表而构建的,它绑定到对象的能力稍后由Bolton提供。这就是为什么得到的是DBNUll而不是null。