vb.net 在组合框中加载两列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18479829/
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
Load two columns in a combo box
提问by user1702346
I have a combo box and I want to fill two columns from sql server database. My code is:
我有一个组合框,我想填充 sql server 数据库中的两列。我的代码是:
Dim cmd As New SqlCommand("Select hospno,hospno + '--- ' + name as pn from Patient order by hospno", cnn)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
If dt.Rows.Count > 0 Then
cbohospno.ValueMember = "hospno"
cbohospno.DisplayMember = "pn"
cbohospno.DataSource = dt
Else
MessageBox.Show("Empty")
End If
While running, combox is showing nameinstead of hospno. How can I show hospnoin combobox?
运行时,combox 显示name而不是hospno. 如何hospno在组合框中显示?
回答by WozzeC
Set the DisplayMember to be "hospno" and you should be fine. DisplayMember is the one that is shown, ValueMember is the value you get if you get the combobox item value.
将 DisplayMember 设置为“hospno”,你应该没问题。DisplayMember 是显示的那个, ValueMember 是您获得组合框项目值时获得的值。

