两个组合框之间的关系 [英] relation between two combobox
问题描述
嗨我的朋友。
我的数据库中有两张表。一个表是City,一个表是State。
表状态,我有两个字段:Id和Name。
在Table city中,我有两个字段:stateid和从状态表中的字段ID中选择stateid的名称。
在vb.net中,我有两个组合框。一个用于州,一个用于城市。
首先我将表状态绑定到组合框但我不知道如何设置状态和城市之间的关系。我在状态组合框中编写了这段代码,但它没有工作。请帮助。
hi my friends.
i have two table im my database. one table is City and one table is State.
in Table State , i have Two field: Id and Name .
in Table city , i have two field : stateid and name that stateid is select from field id in state table.
in vb.net, i have two combobox. one for state and one for city.
first i bind table state to combobox but i don,t know how can set relation between state and city. i writr this code in state combobox but it didn,t work.please help.
da1 = New SqlDataAdapter("select Name from City where City.StateId ='" + Convert.ToString(cmbstate.SelectedValue) + "'", con)
ds1.Clear()
da1.Fill(ds1, "City")
cmbcity.DataSource = ds1
cmbcity.DisplayMember = "City.Name"
推荐答案
在状态组合框的selectionchanged事件中,添加填充所选状态的城市下拉列表。
将以上行更改为以下内容
In the selectionchanged event of the state combo box add the populate the city dropdown for the state selected.
Change the above lines to the following
da1 = New SqlDataAdapter("select Name from City where City.StateId ='" + Convert.ToString(cmbstate.SelectedValue) + "'", con)
ds1.Clear()
da1.Fill(ds1, "City")
cmbcity.DataSource = ds1
cmbcity.DisplayMember = "City.Name"
< br $>
到
to
da1 = New SqlDataAdapter("select Name from City where City.StateId ='" + Convert.ToString(cmbstate.SelectedValue) + "'", con)
ds1.Clear()
da1.Fill(ds1, "City")
cmbcity.DataSource = ds1
cmbcity.DisplayMember = "City.Name"
cmbcity.ValueMember = "City.Name"
你需要做几件事:
- 获取状态并将它们绑定到第一个组合
- 响应到第一个组合的SelectedIndexChanged事件
- 加载相关城市并将它们绑定到第二个组合
You need a couple of things:
- Getting the States and binding them to the first combo
- Responding to the SelectedIndexChanged event of the first combo
- Loading the relevant Cities and binding them to the second combo
Private Sub Form1_Load(sender As Object, e As EventArgs)
FillStates()
End Sub
Private Sub FillStates()
Dim con As New SqlConnection(strConn)
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT StateId, StateName FROM State"
Dim objDs As New DataSet()
Dim dAdapter As New SqlDataAdapter()
dAdapter.SelectCommand = cmd
con.Open()
dAdapter.Fill(objDs)
con.Close()
cmbCountry.ValueMember = "StateId"
cmbCountry.DisplayMember = "StateName"
cmbCountry.DataSource = objDs.Tables(0)
End Sub
Private Sub cmbState_SelectedIndexChanged(sender As Object, e As EventArgs)
If cmbCountry.SelectedValue.ToString() <> "" Then
Dim CountryID As Integer = Convert.ToInt32(cmbCountry.SelectedValue.ToString())
FillStates(CountryID)
cmbCity.SelectedIndex = 0
End If
End Sub
Private Sub FillStates(countryID As Integer)
Dim con As New SqlConnection(strConn)
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT StateID, StateName FROM State WHERE CountryID =@CountryID"
cmd.Parameters.AddWithValue("@CountryID", countryID)
Dim objDs As New DataSet()
Dim dAdapter As New SqlDataAdapter()
dAdapter.SelectCommand = cmd
con.Open()
dAdapter.Fill(objDs)
con.Close()
If objDs.Tables(0).Rows.Count > 0 Then
cmbState.ValueMember = "StateID"
cmbState.DisplayMember = "StateName"
cmbState.DataSource = objDs.Tables(0)
End If
End Sub
Private Sub cmbState_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim stateID As Integer = Convert.ToInt32(cmbState.SelectedValue.ToString())
FillCities(stateID)
End Sub
Private Sub FillCities(stateId As Integer)
Dim con As New SqlConnection(strConn)
Dim cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT CityID, CityName FROM City WHERE StateId =@StateId"
cmd.Parameters.AddWithValue("@StateId", stateID)
Dim objDs As New DataSet()
Dim dAdapter As New SqlDataAdapter()
dAdapter.SelectCommand = cmd
con.Open()
dAdapter.Fill(objDs)
con.Close()
If objDs.Tables(0).Rows.Count > 0 Then
cmbCity.DataSource = objDs.Tables(0)
cmbCity.DisplayMember = "CityName"
cmbCity.ValueMember = "CityId"
End If
End Sub
使用带有两个表的DataSet并定义它们之间的关系时,也应该可以做更聪明的事情但是这将需要您一次加载所有城市。此解决方案类似于延迟加载选项。
It should also be possible to do something more intelligent with a DataSet with two tables and defining the relationship between them but that would require you to load all cities at once. This solution resembles a lazy loading option.
这篇关于两个组合框之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!