vb中的组合框中的两个值 [英] Two values inside a combobox in vb
问题描述
我想要创建一个包含两个字段的组合框。
i want to create a combobox with two fields inside.
到目前为止,这是我的代码。
它只能显示区域数字,但我想在一行显示区域名称。任何人都可以帮助我这个?任何帮助将非常感激。非常感谢你
so far, this is my code.
it can only display the area number but i want to display also the area name in a single line. can anyone help me with this? any help will be greatly appreciated. thank you so much
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
推荐答案
您在绑定
组合框
时犯了一些错误,您是
分配<$DisplayMember
字段以及
ValueMember
字段。因此SelectedItem
和SelectedValue都是
Area_Number。
You are makes some mistake in binding the
combobox
, you are assigningAnum
to bothDisplayMember
field as well as to theValueMember
field. so theSelectedItem
andSelectedValue both are
the Area_Number`.
所以你必须做的是:
重新编码如下的绑定代码片段
So what you have to do is that: Recode the binding snippet as follows
Dim comboarea As New DataTable
comboarea.Load(reader)
cboareano2.DataSource = comboarea
cboareano2.DisplayMember = "Aname"
cboareano2.ValueMember = "Anum"
cboareano2.SelectedIndex = -1
- 您将通过
cboareano2.SelectedValue
-
如果要同时显示代码然后在查询中修改如下:
- Now you can take the Area_Name through
cboareano2.Text
orcboareano2.SelectedItem.Text
- You will get the Area_Code through
cboareano2.SelectedValue
If you want to display both the code and name then make change in the query as follows:
Sql = "select AREA_NO as 'Anum', CONCAT(AREA_NO,'-',AREA_NAME) as 'Aname', AREA_LOCX as 'Alocx' from area"
<and bind the result as i mentioned above.
这篇关于vb中的组合框中的两个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Now you can take the Area_Name through
您可以通过
cboareano2.Text
或取得Area_Name
cboareano2.SelectedItem.Text
获得Area_Code