0

A datagridview(Mydgv1) is created on run time. The first column in it is an editable combo box column.I have set the display member and value member for it, but i am not able to fetch the right display/value member. When I choose an item from its drop down, i get the value of value/display member as the value/display member of the last item in the drop down.Why isn't the value /display member changing with the selected item in combo box column.

On Form Load Event

combo.HeaderText = "Item"
combo.Name = "itemid"
combo.Items.Clear()
Dim ds As SqlDataReader
Dim cmm As New SqlCommand("select itemid from itemdesc", con)
con.Open()
    ds = cmm.ExecuteReader
    If ds.HasRows Then
        While ds.Read
            combo.Items.Add(ds(0).ToString)
            combo.ValueMember = ds("itemid")
            combo.DisplayMember = ds("itemid")
        End While
    End If
con.Close()
Mydgv1.Columns.Add(combo)

On comboboxcolumn's Leave Event

it = combo.DisplayMember
MsgBox(it)
A Koul
  • 33
  • 2
  • 10

1 Answers1

0

do not refer to the displaymember itself and use the SelectedIndexChanged Event to retrieve the selected values

Private Sub combo_SelectedIndexChanged(sender as Object, e as EventArgs) Handels combo.SelectedIndexChanged

    'because your display and valuemember are set as the same
    MsgBox(combo.SelectedValue.ToString)
    'if you want the displaymember as the result use
    MsgBox(combo.Text)

End Sub
Markus
  • 693
  • 8
  • 23
  • The combo is declared globally(forgot t add that code). I cannot type combo.text or combo.selectedvalue . These two properties do not exist for combo.These properties do not show in suggestions pop-up while typing.Since, combo is a programatically created column, and i don't have any experience dealing with them.Do i need to add properties for combo explicitly, I expected that combo, being a combo box type column will have everything a normal combo box has. Thank you for helping me. – A Koul Sep 09 '14 at 09:55
  • do you declare the combobox like so? Private WithEvents combo As ComboBox – Markus Sep 09 '14 at 09:59
  • No, I have written Dim combo As New DataGridViewComboBoxColumn and added the leave event of combo box column in Mydgv1_EditingControlShowing event. – A Koul Sep 09 '14 at 10:06
  • Thank you Markus, Your answer worked when i used it in leave event, Many Thanks.Have a nice day . – A Koul Sep 09 '14 at 10:23