C# 组合框 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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将代码转换为属性而不是成员,然后绑定它
Try converting Code as a Property instead of a member and then binding it