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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combobox population of display and value

Status
Not open for further replies.

gharabed

Programmer
Sep 7, 2001
251
US
I know this seems like a ridiculous question but...how do I get a display and a valueMember populated in a combobox using a datareader (ODP)? I can populate a simple list of values into the combobox by looping through the datareader and using the "items.add" method for the portion of the combobox that displays the text I want displayed to the user. But how do I get the valueMember portion of the combobox populated?

For example, I have a simple lookup table with a "code" and a "description" column. I want to display the "description" value but I want to return the "code" value when the user makes a selection.

I tried using datasets but I've been having problems. They won't show up in the properties sheet after I create the dataset so I've resorted to simply writing my own queries and populating the comboboxes myself manually.

Thanks,
Greg

 
Yeah, but that example uses and OLEDbDataAdapter and OleDBConnection. Would it work the same way with and ODP connection and a dataReader? Guess I can try to goof around with it to get it to work.
 
ZmrAbdulla's link leads you in a good direction. The DataReader can be a little cumbersome.

Use a DataAdapter's Fill method to populate a dataset object, then bind the combobox to the dataset. From that link, all you really need is what is below, but if you are using a MS-SQL DB then use the SqlClient objects instead of the Ole ones. (ie, OleDbDataAdapter = Data.SqlClient.SqlDataAdapter, OleDbConnection = Data.SqlClient.SqlConnection)

Code:
    Dim strSQL As String = "Select Code,Description From MyTable"
    Dim Connection As New OleDbConnection("PROVIDER=....")
    Dim DA As New OleDbDataAdapter(strSQL, Connection)
    Dim DS As New DataSet

    DA.Fill(DS, "Codes")

    With ComboBox1
        .DataSource = DS.Tables(0)
        .DisplayMember = "Description"
        .ValueMember = "Code"
        '.SelectedIndex = 0   'OPTIONAL
    End With

Senior Software Developer
 
I'll have to play around with it because I am not using the OleDbConncection. I am using an Oracle database and am using the Oracle Data Provider (ODP).
 
SiriusBlackOp, in your code:

Code:
    With ComboBox1
        .DataSource = DS.Tables(0)
        .DisplayMember = "Description"
        .ValueMember = "Code"
        '.SelectedIndex = 0   'OPTIONAL
    End With

How would I use the ComboBox1_Click event to get the ValueMember value?

Thanks in advance,
RotorTorque
 
SelectedIndex Change Event ?

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top