Selecting a value from a combo box using code
-
I've got an issue with trying to select a value from a combo box, using code. I have a form that opens up, with a bunch of controls. This form represents a record in a table. I'm currently using a dataset to go and get that data, and then populating the controls with the individual values from that dataset. There is a combo box on the form that is populated using an object that holds the ID of the value, and the display text. These are then assigned to the combo box as ValueMember and DisplayMember respectively. When I ask for the SelectedValue, I get the ValueMember, which is all well and good. However, what I want to do, is when the form loads, I want to return a dataset containing the ID that relates the ValueMember of the combo box, and select the value in the combo box that relates to that value, so that I get the corresponding DisplayMember. I've got no idea how to do this at the moment, and it's driving me potty! Anyone...anyone... Bueller! TIA Tom
-
I've got an issue with trying to select a value from a combo box, using code. I have a form that opens up, with a bunch of controls. This form represents a record in a table. I'm currently using a dataset to go and get that data, and then populating the controls with the individual values from that dataset. There is a combo box on the form that is populated using an object that holds the ID of the value, and the display text. These are then assigned to the combo box as ValueMember and DisplayMember respectively. When I ask for the SelectedValue, I get the ValueMember, which is all well and good. However, what I want to do, is when the form loads, I want to return a dataset containing the ID that relates the ValueMember of the combo box, and select the value in the combo box that relates to that value, so that I get the corresponding DisplayMember. I've got no idea how to do this at the moment, and it's driving me potty! Anyone...anyone... Bueller! TIA Tom
If I understand the problem correctly... You're trying to select a value in a combobox using code? Correct? Notice that SelectedValue is a get/SET property. You can set the value of the SelectedValue property to the ValueMember that you want and it should show you the corresponding DisplayMember in the ComboBox. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
If I understand the problem correctly... You're trying to select a value in a combobox using code? Correct? Notice that SelectedValue is a get/SET property. You can set the value of the SelectedValue property to the ValueMember that you want and it should show you the corresponding DisplayMember in the ComboBox. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Thanks - just by writing a response, trying to show there was nothing wrong with my code, I've realised what I was doing wrong. The class I use to create an object to fill the combo box holds the ID as
Long
, and I was casting the value coming from the dataset as anInteger
. Once I changed the cast to aLong
, it worked like a dream. I'm sorry for wasting your time! Many Thanks Tom