我想创建一个内部有两个字段的组合框。
到目前为止,这是我的代码 它只能显示区号,但我想在一行中显示区域名称。谁能帮我这个?任何帮助将不胜感激。非常感谢你
Sub getarea()
Try
Call MyConnection()
Sql = "select AREA_NO as 'Anum', AREA_NAME as 'Aname', AREA_LOCX as 'Alocx' from area"
Dim cmd As New MySqlCommand(Sql, Con)
Dim reader As MySqlDataReader
reader = cmd.ExecuteReader
Try
Dim comboarea As New DataTable
comboarea.Load(reader)
cboareano2.DataSource = comboarea
cboareano2.DisplayMember = "Anum"
cboareano2.ValueMember = "Anum"
cboareano2.SelectedIndex = -1
Catch ex As Exception
MsgBox(ex.Message)
End Try
Catch ex As Exception
MsgBox(ex.Message)
Con.Close()
End Try
End Sub
Private Sub cboAreaNo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAreaNo.SelectedIndexChanged
Try
Call MyConnection()
Sql = "select AREA_NAME, AREA_LOCX from area where AREA_NO=@Anum"
Dim cmd As New MySqlCommand
With cmd
.CommandText = Sql
.Connection = Con
.Parameters.AddWithValue("@Anum", cboAreaNo.SelectedValue)
.ExecuteNonQuery()
End With
Dim reader As MySqlDataReader
reader = cmd.ExecuteReader
Try
If reader.Read Then
txtlocx.Text = reader.GetString(1)
reader.Close()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Catch ex As Exception
Con.Close()
End Try
答案 0 :(得分:1)
你绑定
combobox
时犯了一些错误,你是 为Anum
字段以及DisplayMember
字段分配ValueMember
SelectedItem
字段。所以SelectedValue both are
和Dim comboarea As New DataTable comboarea.Load(reader) cboareano2.DataSource = comboarea cboareano2.DisplayMember = "Aname" cboareano2.ValueMember = "Anum" cboareano2.SelectedIndex = -1
为Area_Number`。
所以你需要做的是: 重新编码绑定片段如下
cboareano2.Text
cboareano2.SelectedItem.Text
或。来获取Area_Name
cboareano2.SelectedValue
Sql = "select AREA_NO as 'Anum', CONCAT(AREA_NO,'-',AREA_NAME) as 'Aname', AREA_LOCX as 'Alocx' from area"
如果要同时显示代码和名称,请按如下方式对查询进行更改:
username:SessionID:hash
并如上所述绑定结果。