vb.net 使用 ValueMember 属性的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18019933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using the value of the ValueMember property
提问by Blake
I am trying to retrieve the value stored in the "ValueMember" property of a ComboBox. I followed the advice from the following post: How to get ValueMember value from ComboBox C# Winforms?
我正在尝试检索存储在 ComboBox 的“ValueMember”属性中的值。我遵循了以下帖子中的建议:How to get ValueMember value from ComboBox C# Winforms?
Unfortunately it's not working. Below is my code:
不幸的是它不起作用。下面是我的代码:
Private Sub LoadCategoryCombo()
Dim ds As New DataSet
ds = GetCategoryData()
If Not IsNothing(ds) Then
cmbCategory.DataSource = ds.Tables(0)
cmbCategory.DisplayMember = "category"
cmbCategory.ValueMember = "abbrev"
End If
End Sub
What I'm trying to do is When I select a value from the dropdown list, I wish to display the value stored in the "ValueMember" field, however, it just displays "abbrev". What am I doing wrong?
我想要做的是当我从下拉列表中选择一个值时,我希望显示存储在“ValueMember”字段中的值,但是,它只显示“abbrev”。我究竟做错了什么?
Thanks,
谢谢,
回答by Steve
You should use the property Value not ValueMember
您应该使用属性 Value 而不是 ValueMember
Dim valueMemberValue = cmbCategory.SelectedValue.ToString()
The property ValueMemberit is just the name of the column from which you want to retrieve values while DiplayMember is the name of the column from which you want to populate the items collection.
So, setting the ValueMember, you tell the ComboBox to update its Value property when the user select a different item from the Items collection. The combo looks at the corresponding value present in the same row of the item selected but in the column named through the ValueMember property
属性ValueMember它只是您要从中检索值的列的名称,而 DiplayMember 是您要从中填充项目集合的列的名称。
因此,通过设置 ValueMember,当用户从 Items 集合中选择不同的项目时,您告诉 ComboBox 更新其 Value 属性。组合查看所选项目的同一行中存在的相应值,但在通过 ValueMember 属性命名的列中

