Sch*_*oof 2 c# foreach combobox selectedvalue selecteditem
在我的应用程序中,我有一个可以拥有多个位置的客户.当您在我的下拉框中选择一个客户时,它会在一个包含所有位置的flowlayoutpanel中加载组合框.
这是我的代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我无法设置SelectedItem或/和Value.当我查看断点时,有一个值opstapplaats.locatie_id(正确的值),但SelectedValue保持不变null.
我在循环之外做了类似的事情,对于没有在代码中创建的组合框,它在那里工作.

我不知道是什么原因造成的?这是因为它是在一个foreach,因为我在foreach之前使用它然后它工作.
谢谢,托马斯.
编辑:这个问题还没有解决,我不知道如何解决它.
编辑:这个问题似乎已经解决了.见接受的答案.
事实证明你必须首先将控件添加到面板,然后设置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;
Run Code Online (Sandbox Code Playgroud)
然后它工作,我希望这可以帮助别人!
| 归档时间: |
|
| 查看次数: |
9903 次 |
| 最近记录: |