组合框items.count犯规匹配的数据源(C#) [英] ComboBox items.count doesnt match datasource (C#)

查看:98
本文介绍了组合框items.count犯规匹配的数据源(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆