C#:如何通过Enum将HashTable绑定到ComboBox作为密钥?

时间:2010-06-09 14:56:16

标签: c# combobox enums hashtable bindingsource

public static Hashtable m_results = new Hashtable();
private BindingSource m_bindResults = new BindingSource();

// in static constructor
m_results.Add(MyResultTypes.Failed, "Failed");
m_results.Add(MyResultTypes.Pending, "Is Pending");
m_results.Add(MyResultTypes.Completed, "Was Completed");
m_results.Add(MyResultTypes.Cancel, "Cancel it");
m_defaultResult = MyResultTypes.Pending;

// in instance constructor
m_bindResults.DataSource = m_results;
comboResult.DataSource = m_bindResults;
comboResult.ValueMember = "Key";
comboResult.DisplayMember = "Value";
comboResult.SelectedValue = m_defaultTimeoutResult;

上面的代码不起作用:)它用于在哈希表中使用字符串而不是枚举MyResultTypes,并且它正在工作。现在发生的是组合框填充哈希表的值(如我所愿),但未选择默认选定值。

如何在此示例中使用枚举?感谢

编辑:抱歉,ComboTOResult是comboResult,错过了

编辑2 :抱歉,它确实有效。我的不好

1 个答案:

答案 0 :(得分:2)

当我将最后一行更改为

时,对我有用
comboResult.SelectedValue = m_defaultResult; 

ComboTOResult可能是一个不同的盒子?