将标签和值绑定到 ComboBox Winforms
我有这个代码
Public Sub FillCategoryCombobox(ByVal categoryList As List(Of tblCategory), ByVal LvName As ComboBox)
LvName.Items.Clear()
Dim itemValue = New Dictionary(Of Integer, String)()
For Each category As tblCategory In categoryList
itemValue.Add(category.CategoryID, category.CategoryName)
Next category
LvName.DataSource = New BindingSource(itemValue, Nothing)
LvName.DisplayMember = "Value"
LvName.ValueMember = "Key"
End Sub
,我收到一个关于
LvName.DataSource = New BindingSource(itemValue, Nothing)
值不能为空的错误
I have this code
Public Sub FillCategoryCombobox(ByVal categoryList As List(Of tblCategory), ByVal LvName As ComboBox)
LvName.Items.Clear()
Dim itemValue = New Dictionary(Of Integer, String)()
For Each category As tblCategory In categoryList
itemValue.Add(category.CategoryID, category.CategoryName)
Next category
LvName.DataSource = New BindingSource(itemValue, Nothing)
LvName.DisplayMember = "Value"
LvName.ValueMember = "Key"
End Sub
I receive an error on
LvName.DataSource = New BindingSource(itemValue, Nothing)
Value cannot be null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用字典的 ToList() 方法将字典绑定到数据源。
编辑
一些代码:
You can bind a dictionary to a datasource by using the ToList() method of the dictionary.
Edit
Some code:
从未尝试过将字典绑定到控件的数据源或绑定源。
也许那是不可能的。
为什么不使用你的categoryList作为数据源(用于BindingSource或直接)
或者如果你需要维护位置:
或者创建一个
List(of Category)
而不是字典。顺便提一句。完整的堆栈跟踪总是有帮助的。
Never ever tried to bind a dictionary to a control's datasource or bindingsource.
Maybe that's not possible.
Why don't you use your categoryList as a DataSource (for the BindingSource or directly)
or if you need to maintain the position:
or create a
List(of category)
instead of a Dictionary.btw. a full stack trace is always helpfull.
您需要 BindingSource 吗?如果没有,您可以直接将 ComboBox DataSource 设置为您的列表。您可以使用更简单的东西,例如 KeyValuePair,而不是使用字典。
您可以尝试以下操作:
Do you need the BindingSource? If not you can set the ComboBox DataSource to your list directly. And instead of using a dictionary you can use something simpler like a KeyValuePair.
Can you try the following: