MarkItZero Posted April 17, 2003 Posted April 17, 2003 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... '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! Quote
*Experts* Nerseus Posted April 17, 2003 *Experts* Posted April 17, 2003 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. -Nerseuswindowsapplication2.zip Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
MarkItZero Posted April 18, 2003 Author Posted April 18, 2003 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... '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 Quote
*Experts* Nerseus Posted April 18, 2003 *Experts* Posted April 18, 2003 Try this: ' <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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
MarkItZero Posted April 18, 2003 Author Posted April 18, 2003 It works perfectly! Thank you very much! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.