4

I am trying to get the key of SelectedItem of ComboBox but do not figure it out how to get code which I have done is,

void CboBoxSortingDatagridview(ComboBox sender)
{
    foreach (var v in DictionaryCellValueNeeded)
    {
        if (!DictionaryGeneralUsers.ContainsKey(v.Key) && v.Value.RoleId == Convert.ToInt32(((ComboBox)sender).SelectedItem)) // here getting value {1,Admin} i want key value which is 1 but how?
        {
            DictionaryGeneralUsers.Add(v.Key, (GeneralUser)v.Value);
        }
    }
    dataGridViewMain.DataSource = DictionaryGeneralUsers.Values;
}  

I binded the combo box in this way,

cboRolesList.DataSource = new BindingSource(dictionaryRole, null);  
cboRolesList.DisplayMember = "Value";  
cboRolesList.ValueMember = "Key";
Dmitry
  • 13,797
  • 6
  • 32
  • 48
Hassaan
  • 3,931
  • 11
  • 34
  • 67

2 Answers2

15

In cases like this, dictionaries are simply collections of key-value pairs, so each item on the ComboBox is a KeyValuePair<YourKeyType, YourValueType>. Cast SelectedItem to a KeyValuePair<YourKeyType, YourValueType> and then you can read the key.

// get ComboBox from sender
ComboBox comboBox = (ComboBox) sender;

// get selected KVP
KeyValuePair<YourKeyType, YourValueType> selectedEntry
    = (KeyValuePair<YourKeyType, YourValueType>) comboBox.SelectedItem;

// get selected Key
YourKeyType selectedKey = selectedEntry.Key;

Or, a simpler way is to use the SelectedValue property.

// get ComboBox from sender
ComboBox comboBox = (ComboBox) sender;

// get selected Key
YourKeyType selectedKey = (YourKeyType) comboBox.SelectedValue;
Michael Gunter
  • 12,528
  • 1
  • 24
  • 58
  • Your simpler way gives an InvalidCastException. – iheanyi May 18 '19 at 01:02
  • @iheanyi Did you set `ValueMember`, as shown in the original post? From [the documentation of SelectedValue](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listcontrol.selectedvalue?view=netframework-4.8#System_Windows_Forms_ListControl_SelectedValue): "If a property is not specified in ValueMember, SelectedValue returns the results of the ToString method of the object." – Michael Gunter May 21 '19 at 16:37
  • I retract my previous comment. It turns out that I had not set `ValueMember`. I actually had two combo boxes, copy-pasted code and forgot to change the combo box name on the one whose `ValueMember` I actually cared about. The names were similar enough that it took a while to realize my error, even after making several edits to the code. – iheanyi May 21 '19 at 17:06
  • I'd tried it out and got the error referenced in my first comment. Convinced that I'd done nothing wrong, I downvoted your answer. I only realized my error after it was too late to undo the downvote w/o requiring an edit to your answer. – iheanyi May 24 '19 at 17:05
  • @iheanyi You can undo the downvote just by clicking the downvote arrow again. – Michael Gunter May 29 '19 at 15:50
  • After a certain period of time, you can't undo a downvote that way unless the answer is edited. We passed that period before I realized I was wrong. – iheanyi May 30 '19 at 16:03
  • https://meta.stackexchange.com/questions/139949/unable-to-undo-downvote – iheanyi May 30 '19 at 16:05
  • Ah, gotcha! I never knew that rule. No big deal. :) – Michael Gunter May 30 '19 at 17:51
3

Try this:

string key = ((KeyValuePair < string, string > )comboBox1.SelectedItem).Key;

Chhornkimchheng
  • 197
  • 2
  • 12
  • I couldnt get past the InvalidCastException from the answer above (I am sure my fault), This one worked well. Thanks – Grayson Mar 12 '21 at 16:37