Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set selected in a ComboBox, based on ValueMember?

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.

like image 782
Alexandru Pupsa Avatar asked Aug 04 '14 09:08

Alexandru Pupsa


People also ask

Which method is used to get the selected item in ComboBox control?

The ComboBox class searches for the specified object by using the IndexOf method.

How do I index my ComboBox?

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.

How do I set ComboBox to readonly?

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.


2 Answers

Use Following:

comboBoxCustomers.SelectedValue = fld_id(which you are getitng from another source)
like image 80
Tithi Patel Avatar answered Oct 19 '22 19:10

Tithi Patel


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.

like image 36
LuisEduardoSP Avatar answered Oct 19 '22 20:10

LuisEduardoSP