ComboBox.ValueMember and DisplayMember
本文关键字:DisplayMember and ValueMember ComboBox | 更新日期: 2023-09-27 18:26:25
如何设置这些值?我有一个DataTable,里面有我想在组合框中设置的所有数据,但我找不到如何设置。
我试过
ComboBox1.DataSource = dataTable;
ComboBox1.ValueMember = "id"; // --> once hes here, he just jumps out the method
ComboBox1.DisplayMember = "name";
没有编译错误,没有警告,什么都没有。。跳出来!
这是填写DataTable 的查询
"Select * from '"Table'""
我与调试器进行了检查,数据表已填充。列名为"id"answers"name"。组合框为空。我是第一次填充它!
您不应该按的顺序设置列表框和/或组合框的datasource
ComboBox1.DataSource = dataTable;
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
相反,这是正确的顺序:
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
ComboBox1.DataSource = dataTable;
注意:设置datasource
应为最后一行。
如果您先设置datasource
,SelectedIndexChanged
事件将触发,您可能会得到强制转换错误或其他异常。
使用键值对填充组合框
填充组合框的一种巧妙方法是将数据源设置为键值对列表。它也可能启发使用存储在某种列表中的数据:
//Some values to show in combobox
string[] ports= new string[3] {"COM1", "COM2", "COM3"};
//Set datasource to string array converted to list of keyvaluepairs
combobox.Datasource = ports.Select(p => new KeyValuePair<string, string>(p, p)).ToList();
//Configure the combo control
combobox.DisplayMember = "Key";
combobox.ValueMember = "Value";
combobox.SelectedValue = ports[0];
数据源也可以使用以下语法填充:
ports.Select(p => new { Key = p, Value = p }).ToList();
technicue可以扩展为多个列列表的更多属性名称。
已经是键值对的对象(如Dictionary项)可以直接使用
combobox.DataSource = new Dictionary<int, string>()
{
{0, "COM1"},
{1, "COM2"},
{2, "COM3"},
}.ToList();
combobox.ValueMember = "Key";
combobox.DisplayMember = "Value";
元组可以像这样初始化和使用
var ports= new List<Tuple<int, string>>()
{
Tuple.Create(0, "COM1"),
Tuple.Create(1, "COM2"),
Tuple.Create(2, "COM3")
};
combobox.DataSource = ports;
combobox.ValueMember = "Item1";
combobox.DisplayMember = "Item2";
ComboBox1.DataSource= dt; //the data table which contains data
ComboBox1.ValueMember = "id"; // column name which you want in SelectedValue
ComboBox1.DisplayMember = "name"; // column name that you need to display as text
它们接受字符串。。。
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
我也遇到了同样的麻烦。在我的例子中,SelectedIndexChanged事件会触发并跳出该方法。尝试不使用SelectedIndexChanged事件。或者类似的东西:
ComboBox1.SelectedIndexChanged -= new System.EventHandler(ComboBox1_SelectedIndexChanged);
ComboBox1.DataSource = dataTable;
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
ComboBox1.SelectedIndexChanged += new System.EventHandler(ComboBox1_SelectedIndexChanged);
它对我有效。=)
ComboBox1.ValueMember = dataTable.Columns["id"].ColumnsName; // column name which the values are not visible
ComboBox1.DisplayMember = dataTable.Columns ["name"].ColumnsName;
/*
column name that you need to select item by proprity :
ComboBox1.SelectedItem;
Or you can use easly this :
ComboBox1.Text;
*/
ComboBox1.DataSource= dataTable; //the data table which contains data
// and this should be last :)
public class ComboDeger {
private string yazi;
private int deger;
public ComboDeger(string stryazi, int strdeger) {
this.yazi = stryazi;
this.deger = strdeger;
}
public string yazisi {
get {
return yazi;
}
}
public int degeri {
get {
return deger;
}
}
}
private void combobox_doldur() {
ArrayList ComboDegerleri = new ArrayList();
ComboDegerleri.Add(new ComboDeger("9 : NORMAL", 9));
ComboDegerleri.Add(new ComboDeger("10 : ENGELLİ", 10));
comboBox1.DataSource = ComboDegerleri;
comboBox1.DisplayMember = "yazisi";
comboBox1.ValueMember = "degeri";
}
private void Form3_Load(object sender, EventArgs e) {
con.Open();
combobox_doldur();
// Populate the COMBOBOX using an array as DataSource.
}
您可以像这样指定
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
也许不是所描述的问题,但在我的情况下,类型名称显示在组合框中,而不是DisplayValue中,尽管一切似乎都连接正确。结果发现,类型的公共属性是在没有getter和setter的情况下定义的!:)