Populating a Combo Box

MarkItZero

Freshman
Joined
Apr 10, 2003
Messages
43
Location
under the desk in my office
Hello

I currently have a combo box which is filled with JobNames. JobNames is a column in the table Jobs in my Access Database. JobID is the primary key of that table, it is an autonumber.

The JobName that the user selects from the combo box determines the contents of the next form. This works fine, unless of course two Jobs share the same name.

What I would like to do, is populate the combo box with the JobNames and JobID, however the JobID should be hidden.

Here is the code I use to populate the Combo Box...

Code:
'Fill DSJobNames1 Dataset
        OleDbDataAdapter1.Fill(DsJobNames1, "Jobs")

        'Create Dataview
        Dim dm As DataTable = DsJobNames1.Tables("Jobs")
        Dim DVCmboJobNames As DataView = New DataView(dm)
       
        'Populate Combo Box
        cmboJobNames.DataSource = DVCmboJobNames
        cmboJobNames.DisplayMember = "JobName"

Any suggestions?

Thanks!
 
You'll need to create another class and add the class object to your combo instead.

I've attached a sample that shows what I mean. There's a class I created called LookupItem (in LookupItem.vb). I add a couple of instances to an ArrayList and bind the combo to the ArrayList.

-Nerseus
 

Attachments

Thank you Nerseus,
This is very helpful.

I am having trouble adapting your idea to allow me to populate the array with the data from the database. This is what I have to start...

Code:
'Fill DSJobNames1 Dataset
        OleDbDataAdapter1.Fill(DsJobNames1, "Jobs")

        'Create Dataview
        Dim dm As DataTable = DsJobNames1.Tables("Jobs")
        Dim DVCmboJobNames As DataView = New DataView(dm)
       
        'Populate Combo Box
        'cmboJobNames.DataSource = DVCmboJobNames
        'cmboJobNames.DisplayMember = "JobName"

        'Array 
        Dim ArryJobNames As ArrayList = New ArrayList()
        Dim i As Integer

        For i = 0 To DVCmboJobNames.Count - 1
            ArryJobNames.Add(New LookupItem("JobId", "JobName"))
        Next i

        cmboJobNames.DataSource = ArryJobNames
        cmboJobNames.DisplayMember = "Text"
        cmboJobNames.ValueMember = "ID"

Of course this code only fills the array with the string "JobName".

Do you have any suggestions on how to fill the array with the rows from the dataview?

Thank you again
 
Try this:

Visual Basic:
        ' <snipped from your code sample>
        OleDbDataAdapter1.Fill(DsJobNames1, "Jobs")

        Dim ArrayJobNames As ArrayList = New ArrayList()
        Dim row As DataRow
        For Each row In DsJobNames1.Tables("Jobs").Rows
            ArrayJobNames.Add(New LookupItem(row("JobId"), row("JobName").ToString()))
        Next

        'Populate Combo Box
        cmboJobNames.DataSource = ArrayJobNames
        cmboJobNames.DisplayMember = "Text"
        cmboJobNames.ValueMember = "ID"

The DisplayMember and ValueMember need to be the names of the properties of the object that's being bound to. Since you're binding to an ArrayList of LookupItem objects, the combo will see the properties of the LookupItem, not of the DataTable. That's why Displaymember and ValueMember are Text and ID and not JobId and JobName. The JobId and JobName strings are only used to get the right columns from the DataTable into a LookupItem object.

Have fun!

-Ner
 
Back
Top