C#combobox ValueMember

时间:2010-09-08 04:10:34

标签: c# combobox valuemember

我在设置ComboBox的ValueMember属性时遇到了一些问题。

comboBox1.ValueMember = "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);
    }
}

1 个答案:

答案 0 :(得分:3)

尝试将代码转换为属性而不是成员,然后将其绑定