ComboBox.ValueMember 和 DisplayMember [英] ComboBox.ValueMember and DisplayMember
问题描述
我如何设置这些值?我有一个包含要在组合框中设置的所有数据的数据表,但我找不到如何设置它.
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.
我试过了
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!
这是填充DataTable的查询
This is the query to fill the DataTable
"Select * from "Table""
我检查了调试器,数据表已填满.列名称是id"和name".组合框为空.我是第一次填写!
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!
推荐答案
你不应该按照这个顺序设置你的列表框和/或组合框的datasource
You should not set datasource
of your listbox and/or combobox in this order
ComboBox1.DataSource = dataTable;
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
相反,这是正确的顺序:
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
ComboBox1.DataSource = dataTable;
注意:设置datasource
应该在最后一行.
NOTE: setting datasource
should be last line.
如果您先设置 datasource
,SelectedIndexChanged
事件将触发,您可能会收到转换错误或其他异常.
If you set datasource
first, SelectedIndexChanged
event will fire and you may get the cast error or other exception.
这篇关于ComboBox.ValueMember 和 DisplayMember的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!