如何在VB.NET中级联3个组合框,国家,州,城市 [英] How to cascade 3 comboboxes, country, state, city in VB.NET

查看:128
本文介绍了如何在VB.NET中级联3个组合框,国家,州,城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Vb.Net感兴趣的品牌newbbie。我有三个级联组合框,国家,省和市。在国家/地区组合框中进行选择时,我需要自动填充州和城市组合框。组合框从Ms Access数据库中的表中获取数据(通过外键相关)。目前国家comboBox是一个填充,而另外两个没有。我请帮助。



我尝试过:



我尝试过以下代码:

 mports System.Data 
Imports System.Configuration
Imports System.Data.OleDb

Public Class Form1
Dim strConn As String =Provider = Microsoft.Ace.Oledb.12.0; Data source =& Environment.CurrentDirectory& \ _Practice_Country_combo1.accdb
Dim cn As New OleDbConnection(strConn)
Dim Cmd As OleDbCommand
Private Sub Form1_Load_1(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load
LoadCountry()
End Sub
Private Sub LoadCountry()
使用cn作为新的OleDbConnection(strConn)
使用cmd作为新的OleDbCommand()
尝试
使用cmd
.Connection = cn
.CommandType = CommandType.Text
.CommandText =SELECT CountryID,Country FROM Country
以$ b结尾$ b Dim ds As New DataSet()
Dim da As New OleDbDataAdapter()
da.SelectCommand = cmd
cn.Open()
da.Fill(ds)
cn.Close()
ComboBox1.Va lueMember =CountryID
ComboBox1.DisplayMember =Country
ComboBox1.DataSource = ds.Tables(0)
Catch ex As Exception

End Try
结束使用
结束使用
结束Sub

Private Sub LoadProvince(ByVal countryID As Integer)
使用cn作为新OleDbConnection(strConn)
使用Cmd As New OleDbCommand()
尝试
使用Cmd
.Connection = cn
.CommandType = CommandType.Text
.CommandText =SELECT ProvinceID,省FROM Province WHERE CountryID =?

结束Cmd.Parameters.AddWithValue(?CountryID,OleDbType.Integer)
Dim ds As New DataSet()
Dim da As New OleDbDataAdapter()
da.SelectCommand = Cmd
cn.Open()
da.Fill(ds)
cn.Close()
如果ds.Tables(0).Rows.Count> ; 0然后
ComboBox2.ValueMember =省份ID
ComboBox2.DisplayMember =省
ComboBox2.DataSource = ds.Tables(0)
结束如果
Catch ex作为例外

结束尝试
结束使用
结束使用
结束子

私有子LoadCity(ByVal ProvinceID作为整数)
使用cn作为新的OleDbConnection(strConn)
使用cmd作为新的OleDbCommand()
尝试
使用cmd
.Connection = cn
.CommandType = CommandType.Text
.CommandText =SELECT CityID,City FROM City WHERE ProvinceID =?
结束
cmd.Parameters.AddWithValue(?ProvinceID,OleDbType.Integer)
Dim ds As New DataSet()
Dim da As New OleDbDataAdapter()
da.SelectCommand = cmd
cn.Open()
da.Fill(ds)
cn.Close()
如果ds.Tables(0).Rows.Count> ; 0然后
ComboBox3.DataSource = ds.Tables(0)
ComboBox3.DisplayMember =City
ComboBox3.ValueMember =CityID
结束如果
Catch ex作为例外

结束尝试
结束使用
结束使用
结束子

私人子ComboBox1_SelectedIndexChanged(ByVal发送者作为对象,ByVal e As EventArgs)
如果ComboBox1.SelectedValue.ToString()<> 然后
Dim CountryID As Integer = Convert.ToInt32(ComboBox1.SelectedValue.ToString())
LoadProvince(CountryID)
ComboBox3.SelectedIndex = 0
End if
End Sub

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object,ByVal e As EventArgs)
Dim ProvinceID As Integer = Convert.ToInt32(ComboBox2.SelectedValue.ToString())
LoadCity(ProvinceID)
结束子



结束类

解决方案

< blockquote>只需输入如下的LoadCountry()函数





 如果  Page.IsPostBack 那么 
LoadCountry()

结束 如果





以及请更正命令que的参数名称ry



 CommandText =  选择省份ID,省份来自省份国家ID =? 





to



 CommandText =   SELECT ProvinceID,省FROM FROM WHERE CountryID =?CountryID 


I am brand newbbie interested in Vb.Net. I have three cascading comboBoxes , Country, Province and City. I need to have the state and city comboBoxes populated automatically when a selection is made on the country comboBox. The comboBoxes get the data from tables in Ms Access database (related through foreign keys). At the moment the country comboBox is the one populating and nothing on the other two.I Please help.

What I have tried:

I have tried the following code:

mports System.Data
Imports System.Configuration
Imports System.Data.OleDb

Public Class Form1
    Dim strConn As String = "Provider= Microsoft.Ace.Oledb.12.0; Data source =" & Environment.CurrentDirectory & "\Practice_Country_combo1.accdb"
    Dim cn As New OleDbConnection(strConn)
    Dim Cmd As OleDbCommand
    Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadCountry()
    End Sub
    Private Sub LoadCountry()
        Using cn As New OleDbConnection(strConn)
            Using cmd As New OleDbCommand()
                Try
                    With cmd
                        .Connection = cn
                        .CommandType = CommandType.Text
                        .CommandText = "SELECT CountryID, Country FROM Country"
                    End With
                    Dim ds As New DataSet()
                    Dim da As New OleDbDataAdapter()
                    da.SelectCommand = cmd
                    cn.Open()
                    da.Fill(ds)
                    cn.Close()
                    ComboBox1.ValueMember = "CountryID"
                    ComboBox1.DisplayMember = "Country"
                    ComboBox1.DataSource = ds.Tables(0)
                Catch ex As Exception

                End Try
            End Using
        End Using
    End Sub

    Private Sub LoadProvince(ByVal countryID As Integer)
        Using cn As New OleDbConnection(strConn)
            Using Cmd As New OleDbCommand()
                Try
                    With Cmd
                        .Connection = cn
                        .CommandType = CommandType.Text
                        .CommandText = "SELECT ProvinceID, Province FROM Province WHERE CountryID =?"
                    End With
                    Cmd.Parameters.AddWithValue("?CountryID", OleDbType.Integer)
                    Dim ds As New DataSet()
                    Dim da As New OleDbDataAdapter()
                    da.SelectCommand = Cmd
                    cn.Open()
                    da.Fill(ds)
                    cn.Close()
                    If ds.Tables(0).Rows.Count > 0 Then
                        ComboBox2.ValueMember = "ProvinceID"
                        ComboBox2.DisplayMember = "Province"
                        ComboBox2.DataSource = ds.Tables(0)
                    End If
                Catch ex As Exception

                End Try
            End Using
        End Using
    End Sub

    Private Sub LoadCity(ByVal ProvinceID As Integer)
        Using cn As New OleDbConnection(strConn)
            Using cmd As New OleDbCommand()
                Try
                    With cmd
                        .Connection = cn
                        .CommandType = CommandType.Text
                        .CommandText = "SELECT CityID, City FROM City WHERE ProvinceID =?"
                    End With
                    cmd.Parameters.AddWithValue("?ProvinceID", OleDbType.Integer)
                    Dim ds As New DataSet()
                    Dim da As New OleDbDataAdapter()
                    da.SelectCommand = cmd
                    cn.Open()
                    da.Fill(ds)
                    cn.Close()
                    If ds.Tables(0).Rows.Count > 0 Then
                        ComboBox3.DataSource = ds.Tables(0)
                        ComboBox3.DisplayMember = "City"
                        ComboBox3.ValueMember = "CityID"
                    End If
                Catch ex As Exception

                End Try
            End Using
        End Using
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        If ComboBox1.SelectedValue.ToString() <> "" Then
            Dim CountryID As Integer = Convert.ToInt32(ComboBox1.SelectedValue.ToString())
            LoadProvince(CountryID)
            ComboBox3.SelectedIndex = 0
        End If
    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim ProvinceID As Integer = Convert.ToInt32(ComboBox2.SelectedValue.ToString())
        LoadCity(ProvinceID)
    End Sub



End Class

解决方案

Just put LoadCountry() function like below


If Not Page.IsPostBack Then
    LoadCountry()
           
 End If



as well as please correct the parameter name of command query

CommandText = "SELECT ProvinceID, Province FROM Province WHERE CountryID =?"



to

CommandText = "SELECT ProvinceID, Province FROM Province WHERE CountryID =?CountryID"


这篇关于如何在VB.NET中级联3个组合框,国家,州,城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆