I have a combobox with the following structure. Also, I am getting a fld_id from another source and based on that id I need to select the corresponding item in the ComboBox. How can I do that?
comboBoxCustomers.DataSource = customers;
comboBoxCustomers.ValueMember = "fld_id";
comboBoxCustomers.DisplayMember = "fld_name";
Example:
List could contain these items
fld_id fld_name
65 Item1
68 Item2
69 Item3
I need to set Item 68 as selected.
The ComboBox class searches for the specified object by using the IndexOf method.
The Combobox widget allows you to create a dropdown list in which the list of items can be selected instantly. However, if you want to get the index of selected items in the combobox widget, then you can use the get() method. The get() method returns an integer of the selected item known as the index of the item.
Just change the DropDownStyle to DropDownList . Or if you want it completely read only you can set Enabled = false , or if you don't like the look of that I sometimes have two controls, one readonly textbox and one combobox and then hide the combo and show the textbox if it should be completely readonly and vice versa.
Use Following:
comboBoxCustomers.SelectedValue = fld_id(which you are getitng from another source)
I don't have enough reputation to post a comment. This:
comboBoxCustomers.SelectedValue = fld_id
works great :) But AFTER showing the form, otherwise it will fail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With