1

我在 Visual Studio 2010 中使用 C#。我有 2 个从数据库中提取数据的组合框。代码看起来像这样:

        cbo1.DisplayMember = "Name";
        cbo1.ValueMember = "HROfficeLocationID";
        cbo1.DataSource = offices;
        cbo2.DisplayMember = "Name";
        cbo2.ValueMember = "HROfficeLocationID";
        cbo2.DataSource = offices;

我不断收到此异常:'无法绑定到新的值成员。参数名称:newDisplayMember'。我四处搜索,然后重新组织代码行,以便 cbo.DataSource 出现在 .DisplayMember 和 .ValueMember 之前。它最终看起来像这样:

       cbo1.DataSource = offices;
       cbo1.DisplayMember = "Name";
       cbo1.ValueMember = "HROfficeLocationID";
       cbo2.DataSource = offices;
       cbo2.DisplayMember = "Name";
       cbo2.ValueMember = "HROfficeLocationID";

异常消失了。只是想我会分享。

4

4 回答 4

3

当我使用的内部类将变量设置为“内部”时,我就发生了这种情况。将它们更改为“公共”并且效果很好。

于 2013-09-10T13:17:51.863 回答
2

指定为属性,而不是类中的变量,例如,

public class projectData
{
     public string ProjName { get; set; }
     public string ProjId { get; set; }
}


List<projectData> projects = getProjects();


lBoxFDTProjects.DataSource = projects;

lBoxFDTProjects.ValueMember = "ProjId";
lBoxFDTProjects.DisplayMember = "ProjName";
于 2014-11-26T11:16:33.190 回答
0

某些属性属性也会导致此错误,例如[Browsable(false)]

于 2014-06-10T12:25:18.343 回答
0
public class CmbStringItem
{

    public CmbStringItem(string text, string val)
    {
        Text = text;
        Value = val;
    }

    private string text;

    public string Text
    {
        get {return text;}
        set {text = value;}
    }
    private string val;

    [System.ComponentModel.BrowsableAttribute(true)] // must use
    public string Value
    {
        get {return val;}
        set {val = value;}
    }

    public override string ToString()
    {
        return Text;
    }
}

        List<CmbStringItem> items = new List<CmbStringItem>();
        items.Add(new CmbStringItem("Onula",  "0"));
        items.Add(new CmbStringItem("Jedna",  "1"));
        items.Add(new CmbStringItem("Dva", "2"));
        items.Add(new CmbStringItem("Tri", "3"));

        this.cmbSklad.DataSource = items;

        this.cmbSklad.ValueMember = "Value";
        this.cmbSklad.DisplayMember = "Text";
        this.cmbSklad.SelectedIndex = 0;

// 设置 Chombobox - 显示值

cmbSklad.SelectedValue = "1";

于 2015-08-27T08:51:17.530 回答