ComboBox项目为空,但数据源已满 [英] ComboBox Items Empty but DataSource Full
问题描述
将列表绑定到组合框后,其dataSource.Count为5,但是组合框项计数为0。
怎么可能?
我已经习惯了Web编程,这是Windows窗体。
因此不存在 combo.DataBind();
方法。
这里的问题是,我m尝试以编程方式设置所选项目。由于看不到combo.Items集合,因此无法设置所需的项目。
Update
我需要完全更新:
- 数据源包含7个项目
- 绑定到组合框时,
DisplayMember
和ValueMember
得到适当实现 - 数据绑定后,通过gui,我可以清楚地看到组合框中的7个项目
-
combobox.DataSource.Count = 7
和combobox.Items.Count = 0
所以问题出在这里;因为数据绑定之后,组合框的ItemCollection中没有项目;我无法搜索要匹配的图像并设置合适的图像。
这里是一幅更好理解的图像(但是我很确定我很简单) / p>
在 BindingSource <之前添加
ddl.BindingContext = new BindingContext();
之后/ code>分配,一切正常。
After binding a list to combobox, its dataSource.Count is 5 but, combobox item count is 0. how can it be?
I'm used to Web programming and this is in Windows Forms.
So no combo.DataBind();
method exists.
The problem here is, I'm trying to set the selected item programmatically. Since I don't see the combo.Items collection filled, I cannot set the desired item.
Update
A total update is needed I guess:
- datasource contains 7 items
- when bound to combobox,
DisplayMember
andValueMember
are appropriately implemented - after databound, through the gui, I can clearly see the 7 items in the combobox
combobox.DataSource.Count = 7
andcombobox.Items.Count = 0
So the problem is here; since after databound no items are there in the ItemCollection of combobox; I cannot search for one to match and set the appropriate one.
Here is a image for better understanding (But I'm pretty sure I'm missing sth simple)
After adding ddl.BindingContext = new BindingContext();
before the BindingSource
assignment, everything worked fine.
这篇关于ComboBox项目为空,但数据源已满的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!