如何填充VB.NET多维下拉列表?

时间:2011-08-15 13:12:02

标签: vb.net drop-down-menu executereader

我正在尝试初始化VB.NET中的下拉列表,但我的下拉列表没有填充任何值。我希望DataTextField与下拉列表中的DataValues不同。下拉列表应显示一系列字符串; DataValues应该只是数字。

我该如何实现?这是我现在的代码摘录:

Dim ProductList As New ArrayList()
Dim ProdCodeSearch As String
Dim InstrumentSearch As String

pcSQL = " select distinct instrument_name AS [instrument_name], product_code AS [product_code] from FRUD.tblXref order by instrument_name "
Dim DBConn As SqlConnection
DBConn = New SqlConnection(ConfigurationManager.AppSettings("AMDMetricsDevConnectionString"))
DBConn.Open()
Dim reader As SqlDataReader
Dim DBCommand As New SqlCommand(pcSQL, DBConn)       
reader = DBCommand.ExecuteReader()

While reader.Read()
End While

dProdCodeSearch.Items.Add(reader(0))
dProdCodeSearch.DataTextField = "instrument_name"
dProdCodeSearch.DataValueField = "product_code"
dProdCodeSearch.DataBind()
reader.Close()

2 个答案:

答案 0 :(得分:1)

你能用这样的东西吗?

While reader.Read()
   Dim ListItem as new ListItem
   ListItem.Text = reader(0)
   ListItem.Value = reader(1)
   dProdCodeSearch.Items.Add(ListItem)
End While

它通常对我有用..

答案 1 :(得分:0)

尝试按如下方式更改代码,我假设您使用的是ASP.NET

Dim ProductList As New ArrayList()
Dim ProdCodeSearch As String
Dim InstrumentSearch As String

pcSQL = " select distinct instrument_name AS [instrument_name], product_code AS [product_code] from FRUD.tblXref order by instrument_name "
Dim DBConn As SqlConnection
DBConn = New SqlConnection(ConfigurationManager.AppSettings("AMDMetricsDevConnectionString"))
DBConn.Open()
Dim reader As SqlDataReader
Dim DBCommand As New SqlCommand(pcSQL, DBConn)       
reader = DBCommand.ExecuteReader()

While reader.Read()
    dProdCodeSearch.Items.Add(reader(0))
End While

dProdCodeSearch.DataTextField = "instrument_name"
dProdCodeSearch.DataValueField = "product_code"
reader.Close()