用枚举值填充组合框 [英] populate combobox with enum value
问题描述
在表单加载(编辑操作)时,我想用适当的值选择组合框.这些值属于 LangEnum 类型.
On form load (edit action) I want to combobox be selected with appropriate value. These value is of type LangEnum.
我尝试使用 comboBoxLanguage.SelectedValue = book.Language;
但组合始终填充默认值(首先来自枚举列表).
I tried with comboBoxLanguage.SelectedValue = book.Language;
but combo is always populated with default value (first from enum list).
更新:
book.Language 声明为
book.Language is declared as
public enum EnumLang { English = 1, German = 2, Other = 3 };
我用
comboBoxLang.SelectedItem = (Book.EnumLang)book.Language;
和与
comboBoxLang.SelectedItem = book.Language;
没有任何作用(默认的第一个值(英语)总是被设置),值得一提的是在调试模式 book.Language 设置为 German or Other 但在组合框上选择了英语.
and nothing works (default first value (English) is always set) and worth to mention is that on debug mode book.Language is set To German or Other but English is selected on combobox.
推荐答案
这对我来说是正确的!我也在做同样的事情,您确定 book.Language
字符串与列表中的一项完全匹配吗?
That looks right to me!
I am doing the same thing, are you sure that book.Language
string is an EXACT match to one of the items in the list?
在您尝试 SelectedValue
之前是否填充了列表?
And is the list populated BEFORE you are trying to SelectedValue
?
这篇关于用枚举值填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!