Search code examples
c#comboboxenumshashtablebindingsource

C#: How to bind HashTable to a ComboBox via Enum as a key?


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;

Above code diesn't work :) It use to use strings for keys in hashtable instead of enum MyResultTypes, and it was working. What happens now is the combo box gets filled with values of a hashtable (as I want), but the default selected value isn't being selected.

How can I use enums in this example? thanks

Edit: Sorry, ComboTOResult was comboResult, missed it

Edit 2: Sorry, it does work. My bad


Solution

  • Works for me when I change the last line to

    comboResult.SelectedValue = m_defaultResult; 
    

    ComboTOResult maybe a different box?