ComboBox items.count与数据源不匹配 [英] ComboBox items.count doesn't match DataSource
问题描述
我有一个绑定到数据源的ComboBox。我想根据某些条件将项目动态添加到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
,并且它没有增加-它不等于DataSource的计数。有什么想法我可以在这里做什么?
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屋!