ComboBox items.count 与数据源不匹配 [英] ComboBox items.count doesn't match DataSource
问题描述
我有一个绑定到数据源的组合框.我想根据某些条件将项目动态添加到 ComboBox.所以我所做的是将选项添加到一个新列表中,然后像这样更改 ComboBox 的数据源:
I have a ComboBox that is bound to a DataSource. I want to dynamically add items to the ComboBox based on certain conditions. So what I've done is add the options to a new list, and then change the DataSource of the ComboBox like so:
cbo.DataSource = null;
cbo.DataSource = cbos;
cbo.DisplayMember = "Title";
cbo.ValueMember = "Value";
然后,我检查 cbo.Items.Count
,它没有增加 - 它不等于数据源的计数.任何想法我可以在这里做什么?
Then, I check cbo.Items.Count
, and it has not incremented - it does not equal the count of the DataSource. Any ideas what I can do here?
注意这是 WinForms 而不是 ASP.NET.
Note this is WinForms and not ASP.NET.
推荐答案
如果有人在动态添加的组合框上遇到此问题,答案是确保将组合框添加到表单中容器的控件中.
If anyone experiences this problem on a dynamically added combobox, the answer is to ensure that you add the combobox to the controls of a container in the form.
>
通过添加this.Controls.Add(cbo);"设置数据源之前的代码,问题就解决了.
By adding "this.Controls.Add(cbo);" to the code before setting the datasource, the problem goes away.
这篇关于ComboBox items.count 与数据源不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!