Can't set Combobox SelectedItem, Value
Solution 1
Turns out you have to first add the control to the panel, and then set the ValueMember
, DisplayMember
...
ComboBox cbbOpstap = new ComboBox();
cbbOpstap.Width = 200;
cbbOpstap.Height = 20;
flpOpstapplaats.Controls.Add(cbbOpstap);
cbbOpstap.ValueMember = "locatie_id";
cbbOpstap.DisplayMember = "FullAdress";
bbOpstap.DataSource = LocatieManagement.getLocaties();
cbbOpstap.SelectedValue = opstapplaats.locatie_id;
cbbOpstap.SelectedItem = opstapplaats;
Then it works, I hope this can help somebody!
Solution 2
When you set the Valuemember
and displayMember
, please use SelectedIndex
to select an item.
cbbOpstap.DataSource = Database.getLocaties();
cbbOpstap.ValueMember = "locatie_id";
cbbOpstap.DisplayMember = "FullAdress";
cbbOpstap.SelectedIndex = cbbOpstap.Items.IndexOf(opstapplaats.locatie_id);
Schoof
Updated on June 04, 2022Comments
-
Schoof almost 2 years
In my application I have a customer who can have multiple locations. When you select a customer in my dropdownbox, it will load comboboxes in a flowlayoutpanel with all it's locations.
This is my code for this:
IEnumerable<locatie> opstapPlaatsen = Database.getOpstapplaatsen(klant.klant_id); foreach (locatie opstapplaats in opstapPlaatsen) { if (opstapPlaatsen.Count() <= 0) { } else { ComboBox cbbOpstap = new ComboBox(); cbbOpstap.Width = 200; cbbOpstap.Height = 20; cbbOpstap.DataSource = Database.getLocaties(); cbbOpstap.ValueMember = "locatie_id"; cbbOpstap.SelectedValue = opstapplaats.locatie_id; cbbOpstap.SelectedItem = opstapplaats; cbbOpstap.DisplayMember = "FullAdress"; flpOpstapplaats.Controls.Add(cbbOpstap); } }
My problem is that I can't set the SelectedItem or/and Value. When I look with breakpoints there is a value in
opstapplaats.locatie_id
(the correct one), butSelectedValue
staysnull
.I do something alike outside of a loop, and for a combobox not created in code, and it works there.
I have no idea what's causing this? Is this because it's in a foreach, because I used it before out of a foreach and then it worked.
-
Schoof about 12 yearsThe code works, but it also doesn't change the SelectedIndex, it's still always at the default (-1). I have made a screenshot of the problem, to clarify.
-
PraveenVenu about 12 yearsAre you sure that there is an item with the value opstapplaats.locatie_id in the dropdown?
-
V4Vendetta about 12 years@ThomasSchoof Just check if the value you are passing is in the same case as in the
Items
list. -
Schoof about 12 yearsThere appear to be no items in the combobox, but the datasource is filled! Here you can see the filled datasource, I'm not sure what's causing this?
-
PraveenVenu about 12 yearsis it a webapplication or windows application?
-
Schoof about 12 yearsI do exactly the same thing out of the
foreach
(to just get the adress) and there it just works. i.imgur.com/5g4RR.png -
steven2308 about 9 yearsBy the way it has to be in that same order, i.e. If first you set the ValueMember, DisplayMember and DataSource and then you add the control to the form (which is what I was trying to do since my combo is filled dynamically from different tables and it may not find the source) you won't be able to set the SelectedValue.