如何设置此值?我有一个DataTable,包含我想在组合框中设置的所有数据,但我无法找到如何设置它。
我试过
ComboBox1.DataSource = dataTable;
ComboBox1.ValueMember = "id"; // --> once hes here, he just jumps out the method
ComboBox1.DisplayMember = "name";
没有编译错误,警告,没有......只是跳出来!
这是填充DataTable的查询
"Select * from \"Table\""
我检查了调试器并填充了数据表。列名称是“id”和“name”。 ComboBox是空白的。我是第一次填写它!
答案 0 :(得分:22)
您不应按此顺序设置列表框和/或组合框的datasource
ComboBox1.DataSource = dataTable;
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
相反,这是正确的顺序:
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
ComboBox1.DataSource = dataTable;
注意:设置datasource
应该是最后一行。
如果您先设置datasource
,SelectedIndexChanged
事件将会触发,您可能会收到强制转换错误或其他异常。
答案 1 :(得分:3)
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
答案 2 :(得分:2)
他们采取串......
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
答案 3 :(得分:1)
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.
}
答案 4 :(得分:1)
我遇到了同样的麻烦。在我的例子中,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);
它对我有用。 =)
答案 5 :(得分:1)
使用键值对填充组合框
填充组合框的一种巧妙方法是将数据源设置为键值对列表。它也可能激发使用存储在某种列表中的数据:
//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();
可以使用多个列列表的更多属性名称扩展技术。
答案 6 :(得分:0)
您可以像这样指定
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
答案 7 :(得分:0)
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 :)