Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set ComboBox selected item by Key not by value c#

Before post this question ,I believe it is simple issue , I search for the answer and didn't find suitable solution.

in my daily work , I am working with web applications and can easily get or set values of dropdownlists

i can not do the same in windows application C#

I have combobox and class comboItem

 public class ComboItem
    {
        public int Key { get; set; }
        public string Value { get; set; }
        public ComboItem(int key, string value)
        {
            Key = key; Value = value;
        }
        public override string ToString()
        {
            return Value;
        }
    }

Say the combobox is binded through hard code and the values are

  • Key : 1 / Value : Male
  • Key : 2 / Value : Female

  • Key : 3 / Value : Unknown

lets say i have the Key =3 and I want to set this item ( whose key is 3 ) through code so when form is loaded , the selected value by default will be Unknown.

combobox1.selectedValue =3 //Not Working , selectedValue used to return an object
combobox1.selectedIndex = 2 //Working as 2 is the index of key 3/Unknown

but lets say i don't know the index , how can i get the index of item whose key = 3 ?

index can be get through value in this way

int index = combobox1.FindString("Unknown") //will return 2

FindString take a value not a key , i need something like FindString which take a key and return index

NOTE : Here is how i bind my drop down menu

 JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
                        jsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;

                        var empResult= await response.Content.ReadAsStringAsync();
                        List<Emp> Emps= JsonConvert.DeserializeObject<Emp[]>(empResult, jsonSerializerSettings).ToList();
                        foreach (var item in Emps)
                        {
                            ComboItem CI = new ComboItem(int.Parse(item.ID), item.Name);
                            combobox1.Items.Add(CI);
                        }
                        this.combobox1.DisplayMember = "Value";
                        this.combobox1.ValueMember = "Key";
like image 409
Bassem Avatar asked Jan 27 '26 10:01

Bassem


1 Answers

You need to set the ValueMember property so the ComboBox knows what property to deal with when SelectedValue is being used. By default the ValueMember will be empty. So when you set the SelectedValue, the ComboBox does not know what you want to set.

this.comboBox1.ValueMember = "Key";

Normally, you would also set the DisplayMember property:

this.comboBox1.DisplayMember = "Value";

If you do not set it, it will just call ToString() on the object and display that. In your case the ToString() returns the Value.

how can i get the index of item whose key = 3 ?

If you want the item whose key is 3, why do you need to get it from the combobox? You can just get it from the collection the combobox is bound to:

For example, imagine this:

var items = new List<ComboItem> { new ComboItem(1, "One"),
    new ComboItem( 2, "Two") };

this.comboBox1.DataSource = items;
this.comboBox1.DisplayMember = "Value";
this.comboBox1.ValueMember = "Key";

this.comboBox1.SelectedValue = 2;

If I need the item whose key is 2, then this will achieve that:

// Use Single if you are not expecting a null
// Use Where if you are expecting many items
var itemWithKey2 = items.SingleOrDefault(x => x.Key == 2);
like image 174
CodingYoshi Avatar answered Jan 28 '26 23:01

CodingYoshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!