Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ComboBox Value/Display Member Problem...

Status
Not open for further replies.

combs

Programmer
Apr 18, 2002
78
US
I have a combo box that is filled from a database. The connection seems to work all right and what I "SEE" when the form loads is correct..... However, the value member is not what I expect it to be.

Here's the code:
Code:
    Private Sub SetUpWorkItemsComboBox()
        Dim strDBLocation As String
        strDBLocation = My.Settings.DatabaseLocation

        Dim conn As New OleDb.OleDbConnection
        conn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = " & strDBLocation

        Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM tblWorkItems ORDER BY Item_ID", conn)
        da.Fill(m_DataSet)
        Me.WorkItemComboBox.DataSource = m_DataSet.Tables(0)
        Me.WorkItemComboBox.ValueMember = "UnitMeasure"
        Me.WorkItemComboBox.DisplayMember = "Description"
        m_FormLoaded = True
    End Sub

The problem is that the value member is always "UnitMeasure". Description and UnitMeasure are fields in the table indicated. Here's the code that leads me to think there's an error:

Code:
Private Sub WorkItemComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WorkItemComboBox.SelectedIndexChanged
        Dim strItem As String
        strItem = WorkItemComboBox.Text
        If m_FormLoaded = True Then
            MsgBox(strItem & vbNewLine & WorkItemComboBox.ValueMember.ToString)
        End If
    End Sub

Am I doing something wrong here??

Thanks in advance for your help.
 

Try setting the properties in this order:

Me.WorkItemComboBox.ValueMember = "UnitMeasure"
Me.WorkItemComboBox.DisplayMember = "Description"
Me.WorkItemComboBox.DataSource = m_DataSet.Tables(0)

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
jebenson,

Thanks for your quick response, but still the same result after moving the DataSource statement.


 

Duh! Sorry, I wasn't reading your code closely enough.

This line:

MsgBox(strItem & vbNewLine & WorkItemComboBox.ValueMember.ToString)

Will of course return "UnitMeasure", because that is what you set as the ValueMember for the combobox. What you need to do is get the SelectedValue:

MsgBox(strItem & vbNewLine & WorkItemComboBox.[red]SelectedValue[/red].ToString)



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
jebenson,

Perfect, that was it. My stupid mistake....

Thanks again for your prompt help!
That's exactly why these forums rock!

-Damon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top