组合框items.count犯规匹配的数据源(C#) [英] ComboBox items.count doesnt match datasource (C#)
问题描述
我有一个绑定到一个数据源组合框。我要动态地添加项目在一定条件下的组合框。所以我所做的就是添加选项到一个新的列表,然后改变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
,现在也没有增加 - 这不等于数据源的数量。任何想法我能做些什么吗?请注意,这是WinForms和不asp.net。
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? 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.
这篇关于组合框items.count犯规匹配的数据源(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!