How to set selected item when combobox is bound to dictionary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • doubler
    New Member
    • Nov 2012
    • 4

    How to set selected item when combobox is bound to dictionary

    This seems like it should be fairly simple, but it has me stumped.

    I have a combobox that is bound to a dictionary. I also have a datagridview that is supposed to populate the combobox when a row is selected. The gridview displays the key which is numeric and I want the combobox to display the value which is text. I thought this would work:
    Code:
    cboSourceID.SelectedItem = dgvInfoware.Item(3, rowIndex).Value
    It doesn't. I also thought maybe I needed to set the value member to the new key, but that deleted everything in the combobox.
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    Not knowing exactly how you're binding your ComboBox to your Dictionary have you tried the SelectedValue of the combobox, like this:

    Code:
    cboSourceID.SelectedValue = dgvInfoware.Item(3, rowIndex).Value

    Comment

    • doubler
      New Member
      • Nov 2012
      • 4

      #3
      That doesn't work either. This is how I did the bind.
      Code:
       cboSourceID.BindingContext = New BindingContext
       cboSourceID.DataSource = New BindingSource(dictIDs, Nothing)
       cboSourceID.DisplayMember = "Value"
       cboSourceID.ValueMember = "Key"

      Comment

      Working...