将标签和值绑定到 ComboBox Winforms

发布于 2024-09-24 19:02:14 字数 614 浏览 3 评论 0原文

我有这个代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悟红尘 2024-10-01 19:02:14

您可以使用字典的 ToList() 方法将字典绑定到数据源。

编辑

一些代码:

LvName.DataSource = itemValue.ToList()
LvName.DisplayMember = "Value"
LvName.ValueMember = "Key"

You can bind a dictionary to a datasource by using the ToList() method of the dictionary.

Edit

Some code:

LvName.DataSource = itemValue.ToList()
LvName.DisplayMember = "Value"
LvName.ValueMember = "Key"
不知在何时 2024-10-01 19:02:14

从未尝试过将字典绑定到控件的数据源或绑定源。
也许那是不可能的。
为什么不使用你的categoryList作为数据源(用于BindingSource或直接)

combo1.DataSource = categoryList
combo1.DisplayMember = "CategoryName"
combo1.ValueMember = "CategoryID"

或者如果你需要维护位置:

dim bs as new BindingSource(categoryList, nothing)
combo1.DataSource = bs
combo1.DisplayMember = "CategoryName"
combo1.ValueMember = "CategoryID"    

或者创建一个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)

combo1.DataSource = categoryList
combo1.DisplayMember = "CategoryName"
combo1.ValueMember = "CategoryID"

or if you need to maintain the position:

dim bs as new BindingSource(categoryList, nothing)
combo1.DataSource = bs
combo1.DisplayMember = "CategoryName"
combo1.ValueMember = "CategoryID"    

or create a List(of category) instead of a Dictionary.

btw. a full stack trace is always helpfull.

猫性小仙女 2024-10-01 19:02:14

您需要 BindingSource 吗?如果没有,您可以直接将 ComboBox DataSource 设置为您的列表。您可以使用更简单的东西,例如 KeyValuePair,而不是使用字典。
您可以尝试以下操作:

KeyValuePair[] pairs = new KeyValuePair[0];
ComboBox box = new ComboBox();
box.DisplayMember = "Value";
box.ValueMember = "Key";
box.DataSource = pairs;

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:

KeyValuePair[] pairs = new KeyValuePair[0];
ComboBox box = new ComboBox();
box.DisplayMember = "Value";
box.ValueMember = "Key";
box.DataSource = pairs;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文