1

I'm using C# in Visual Studio 2010. I have 2 comboboxes that pull data from the database. The code looks something like this:

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

I kept getting this exception: 'Cannot bind to the new value member. Parameter name: newDisplayMember'. I searched around and then reorganized the lines of code so that cbo.DataSource came before .DisplayMember and .ValueMember .It ended up looking something like this:

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

The exception went away. Just thought I'd share.

Tatiana Laurent
  • 281
  • 1
  • 4
  • 13

4 Answers4

3

I had this occur when the internal class I was using had the varialbles as "internal". Changed them to "public" and it worked fine.

RCinAL
  • 31
  • 2
2

Specify as a Property, not as a variable in a class for example,

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";
ptsivakumar
  • 437
  • 6
  • 4
0

Some property attributes also cause this error like the [Browsable(false)]

rajeemcariazo
  • 2,476
  • 5
  • 36
  • 62
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;

// set Chombobox - Display vlaue

cmbSklad.SelectedValue = "1";