Search code examples
vb.netcomboboxvaluemember

Populate Combobox from db - ValueMember issue


I cannot find a reason, why in my case a the DisplayMember gets populated correctly and ValueMember gets instead only a string (column name). It's same for different comboboxes, column names and tables.

            Using comm3 As SqlCommand = New SqlCommand("SELECT PID, RTRIM(Desc) as Desc FROM Conds WHERE Typ = 3", oConn)
                Dim rs As SqlDataReader = comm3.ExecuteReader
                Dim dt As DataTable = New DataTable
                dt.Load(rs)
                cbConditionPayment.ValueMember = "PID"
                cbConditionPayment.DisplayMember = "Desc"
                cbConditionPayment.DataSource = dt
            End Using 'comm3

So DisplayMember gets values like "Condition1, Condition2, Condition3,....", while ValueMember gets values "PID, PID, PID, PID,...." instead of "1, 2, 3, 18, 22, ..."

Must be something stupid...

Regards,

Oak


Solution

  • I think that you may have misunderstood what the ValueMember does. How are you actually getting those "PID" values? Are you getting them from the ValueMember property? If so then that's exactly what you'd expect because that's what you assigned to it in the first place.

    The ValueMember is the name of the column from which the SelectedValue gets its value. When the user selects an item by its "Desc" value, the SelectedValue property will return the corresponding "PID" value.