views:

73

answers:

2

Hello guys,

I am experiencing some problems while setting ValueMember property of my ComboBox.

The line comboBox1.ValueMember = "Code"; breaks my code.

Here is my code:

Form1.cs:

private void Form1_Load(object sender, EventArgs e) {  
    ...  
    ...      
    MAPList MAP = new MAPList();  
    comboBox1.DataSource = MAP.All;  
    comboBox1.ValueMember = "Code";  
    ...  
    ...  
}

MAPList.cs:

public class MAPList {  
    public readonly List<MAP> All;

    public MAPList() {
        All = new List<MAP>();

        var MapData = // Getting map data

        foreach(MAP m in MapData) {
            All.Add(new Map(m.Name, m.Code));
        }
    }
}

MAP.cs:

public class MAP {  
    public readonly string Name;  
    public readonly string Code;

    public RadioCode(string name, string code) {
        Name = name;
        Code = code;
    }

    public override string ToString() {
        return String.Format("{0}: {1}", Name, Code);
    }
}

Your help on this would be much appreciated :)

-- Ruby

+3  A: 

Try converting Code as a Property instead of a member and then binding it

Vinay B R
wow... that's strange! why wouldn't it work when Code being a public readonly member?
Ruby
@Ruby The ComboBox uses reflection to get the value from the underlying object. During this process it is looking for Properties and not Fields
Rohan West
A: 

Bind it to Code as a property

pymendoza