Combo Boxes and Databases
Okay, having an issue here. Firstly, this is my first post here; I've been reading the forum for a while.
So, because of a situation at work, I've taken up programming again after not writing any code for 10+ years. I'm working on a "simple" application and I have some questions regarding combo boxes...
My form has 11 combo boxes... Each box is populated via a SQL database table. (that was tricky, but I figured out how)... that much is working.
Here's an example of what I'm trying to do...
THis is basically what my database structure looks like:
Value | Code
Apple | AP
Orange | OR
Kiwi | KI
Right now the combo box is populated with the 'value' column of the table.. what I want is when the user selects Orange from the combo box, I want a Label to change to OR... and so on...
Eventually, as each of the 11 combo boxes are selected, it will create a 17 digit SKU code. via 11 labels.
Thanks for any help!
Re: Combo Boxes and Databases
A Databound combobox has both a .DisplayMember and a .ValueMember. You would therefore set Value as the DisplayMember and Code as the ValueMember (you might want to think about changing your column names or it's going to get really confusing working out what Value means!) Then when a selection is made you would take the CB.SelectedValue as the value to pass on to the Label (see, I told you it would get confusing!)
Re: Combo Boxes and Databases
haha. yeah, I was considering changing the name of the columns in the tables.
but I'm getting an error now. I have a feeling that it is in the syntax though..
Firstly, I already had set the DisplayMember to Value and the ValueMember to Code... I'm having problems passing the Code to the appropriate label.
Here's what I'm doing wrong:
Quote:
Private Sub CboProdFam_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboProdFam.SelectedIndexChanged
lblOutFam.Text = cboProdFam.SelectedValue
End Sub
Re: Combo Boxes and Databases
Here's the whole thing if that helps:
Quote:
Public Class Form1
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SKUDataSet.VolumeLevel' table. You can move, or remove it, as needed.
Me.VolumeLevelTableAdapter.Fill(Me.SKUDataSet.VolumeLevel)
'TODO: This line of code loads data into the 'SKUDataSet.Special' table. You can move, or remove it, as needed.
Me.SpecialTableAdapter.Fill(Me.SKUDataSet.Special)
'TODO: This line of code loads data into the 'SKUDataSet.Bundle' table. You can move, or remove it, as needed.
Me.BundleTableAdapter.Fill(Me.SKUDataSet.Bundle)
'TODO: This line of code loads data into the 'SKUDataSet.TimePeriod' table. You can move, or remove it, as needed.
Me.TimePeriodTableAdapter.Fill(Me.SKUDataSet.TimePeriod)
'TODO: This line of code loads data into the 'SKUDataSet.Pack' table. You can move, or remove it, as needed.
Me.PackTableAdapter.Fill(Me.SKUDataSet.Pack)
'TODO: This line of code loads data into the 'SKUDataSet.PriceGroup' table. You can move, or remove it, as needed.
Me.PriceGroupTableAdapter.Fill(Me.SKUDataSet.PriceGroup)
'TODO: This line of code loads data into the 'SKUDataSet.ProductType' table. You can move, or remove it, as needed.
Me.ProductTypeTableAdapter.Fill(Me.SKUDataSet.ProductType)
'TODO: This line of code loads data into the 'SKUDataSet.CountryRegion' table. You can move, or remove it, as needed.
Me.CountryRegionTableAdapter.Fill(Me.SKUDataSet.CountryRegion)
'TODO: This line of code loads data into the 'SKUDataSet.Version' table. You can move, or remove it, as needed.
Me.VersionTableAdapter.Fill(Me.SKUDataSet.Version)
'TODO: This line of code loads data into the 'SKUDataSet1.ProductCode' table. You can move, or remove it, as needed.
Me.ProductCodeTableAdapter.Fill(Me.SKUDataSet1.ProductCode)
'TODO: This line of code loads data into the 'SKUDataSet.ProductFamily' table. You can move, or remove it, as needed.
Me.ProductFamilyTableAdapter.Fill(Me.SKUDataSet.ProductFamily)
'Set Default Values into the combo boxes of the SKU Group.
Me.cboProdFam.SelectedIndex = 0
Me.cboProdCode.SelectedIndex = 0
Me.cboVersion.SelectedIndex = 0
Me.cboCountryRegion.SelectedIndex = 0
Me.cboProdType.SelectedIndex = 0
Me.cboPriceGroup.SelectedIndex = 0
Me.cboPack.SelectedIndex = 0
Me.cboTimePeriod.SelectedIndex = 0
Me.cboBundleCode.SelectedIndex = 0
Me.cboSpecial.SelectedIndex = 0
Me.CboVolume.SelectedIndex = 0
'Dec Variables for each
End Sub
Private Sub CboProdFam_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboProdFam.SelectedIndexChanged
lblOutFam.Text = cboProdFam.SelectedValue
End Sub
My error is actually 'Class statement must end with a matching 'End Class'... beginning to wonder if I deleted it somewhere...
Re: Combo Boxes and Databases
ah ha! found it. it's working, thanks!
Re: Combo Boxes and Databases
ok, it's better, but has some issues.
When I make a selection from the first combo box, it changes all of the other combo boxes to it's code (which isn't even a valid code)
ie.. using the example above... if I select Orange from the first combo box, it changes the label like I want, but every combobox will have an OR selected... it shouldn't even be populated with OR.
:confused:
Re: Combo Boxes and Databases
Er .. that rather suggests that you're using the same databinding for all the boxes. Which in turn suggests that you have even less of a handle on this business than you suggested (no offence). Having said that this is kinda what you get for design time binding rather than learning the nuts and bolts of database manipulation. It looks like the easy option but it confuses the heck out of me!!
In this circumstance I'm not sure that I would be using bindings at all. You're not actually using the datarows as such, merely random(ish) cell values, so it really doesn't make a lot of sense to be tying yourself to the rigidity of the datatable.