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, help setting display value from data code value through code 2

Status
Not open for further replies.

kizakiza

Programmer
Sep 29, 2006
4
AU
I have a Modal Form where the data entry/edit fields are populated (in code on form load) from the record/row selected and passed as a parameter from a bound DataGridView control on the parent form.

All data fields populate as I require. However there is a data field/item 'codecolour' which I do not want displayed I want the 'Colour Description' hence the use of the ComboBox. I want to give the ComboBox the data value 'ColourCode' and expect the ComboBox to display the 'Colour Description'. All is working great with the exception of my ComboBox values.

The combobox is bound to a lookup table Colours with fields 'ColourCode' and 'Colour Description'

I have the ComboBox working fine for Adding a Record, user selects the Colour via Description and the colour code is correctly provided back for saving in the table. When Editing a record I need the reverse to occur when presenting the data to the user, i.e. somehow give the colourcode value to the combobox and have it automatically lookup the colour table and display the colour description. I want to stress that none of the fields on the child/modal form are bound fields, I am processing them unbound which is what I want. (The ComboBox is bound to the lookup table to show list of possible values.

Seems a simple thing to want to do, but I have spent so much time trying to get it to work, must be missing something. Any help appreciated.
cheers


 
Use to ComboBox's FindString method:

ComboBox1.SelectedIndex = ComboBox1.FindString("<ColorDescription Here>")

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!
 
Since it is bound to a table you likely have properties like this (either in code or design time)
me.Combobox1.Datasource = dataTableColors
me.combobox1.displaymember = "Colour Description"
me.combobox1.ValueMember = "Colour Code"

So then you can simply do
me.Combobox1.selectedvalue = "yourcolorcode
 
Thanks for the replies. I was already doing what "MajP" suggests. The problem was that I had added code to the Form New to handle the passing of the DataGridRow as a parameter to the form so that I could process the DataGridView Row that the user had selected.

What I was doing wrong was that I was using the me.combobox1.SelectedValue="Mycolourcode" before I had filled the tableAdaptor with lookup data which the combo boxes relied on. I did this in the form Load event.

I have moved the me.combobox1.selectedvalue="Mycolourcode" to after the table adaptor fill is called in the form load event (loads the data for the combobox dropdown list) and this solved my problem.

Very simple mistake, but I wasted so much time on it. Thanks Kizza.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top