C# ComboBox.ValueMember 和 DisplayMember
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9521980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ComboBox.ValueMember and DisplayMember
提问by dbncourt
How do i set this values? I have a DataTable with all the data i want to set in the combobox, but i cant find how to set it.
我如何设置这些值?我有一个包含要在组合框中设置的所有数据的数据表,但我找不到如何设置它。
I tried
我试过
ComboBox1.DataSource = dataTable;
ComboBox1.ValueMember = "id"; // --> once hes here, he just jumps out the method
ComboBox1.DisplayMember = "name";
No compilation error, warning, nothing.. just jumps out!
没有编译错误,警告,什么都没有......只是跳出来!
This is the query to fill the DataTable
这是填充数据表的查询
"Select * from \"Table\""
I checked with the debugger and the datatable was filled. The columns names are "id" and "name". ComboBox is blank. I'm filling it for the first time!
我检查了调试器并填充了数据表。列名称是“id”和“name”。组合框为空。我是第一次填!
回答by Austin Salonen
They take strings...
他们拿绳子...
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
回答by Enigma State
you could specify like this
你可以这样指定
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
回答by PraveenVenu
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
回答by Visitor
You should not set datasourceof your listbox and/or combobox in this order
您不应datasource按此顺序设置列表框和/或组合框
ComboBox1.DataSource = dataTable;
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
Instead, this is correct order:
相反,这是正确的顺序:
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
ComboBox1.DataSource = dataTable;
NOTE: setting datasourceshould be last line.
注意:设置datasource应该在最后一行。
If you set datasourcefirst, SelectedIndexChangedevent will fire and you may get the cast error or other exception.
如果您datasource先设置,SelectedIndexChanged事件将触发,您可能会收到转换错误或其他异常。
回答by oKAN
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.
}
回答by Felipe Leal Cruz
I had the same trouble. In my case, SelectedIndexChanged event fires and just jumps out the method. Try do not use SelectedIndexChanged event. Or something like this:
我有同样的麻烦。在我的情况下, 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);
It worked for me. =)
它对我有用。=)
回答by flodis
Using keyvalue pairs to populate a combobox
使用键值对填充组合框
A neat way to populate combo boxes is to set the datasource to a list of keyvalue pairs. It may also inspire using data stored in a list of some kind:
填充组合框的一种巧妙方法是将数据源设置为键值对列表。它也可以激发使用存储在某种列表中的数据:
//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];
The datasource can be populated using this syntax as well:
也可以使用以下语法填充数据源:
ports.Select(p => new { Key = p, Value = p }).ToList();
The technicue may be expanded with more property names for multiple column lists.
该技术可以扩展为多列列表的更多属性名称。
回答by abdellah
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 :)

