如何将州与城市联系起来 [英] how to bind state with city
问题描述
注意:在基于Windows的应用程序(VB.NET)
当用户点击状态组合框时时间
city combobox 已停用,当用户选择州时,相关城市将显示在城市组合框中。
*****************************************数据库i中的************************************
有两个表名为:州和城市
州名称: State_ID 和 State_Name
城市的字段名称: City_ID ,
State_Id 和
************************ ************************************************** ***
您好,
以下代码示例(vb.net中的完整源代码)使用我在网络上找到的数据库。
I写了下面的代码
用于读取数据的类,注意我有限制数据,因为表格非常大以保持轻量级。请注意,SQL-Server的数据源需要从KARENS-PC更改为您的服务器名称,或者如果使用SQL-SERVER的Express版本更改为。\SQLEXPRESS。
如果使用MS-Access,代码逻辑是相同的,但您需要调整为使用OleDb而不是SqlClient数据提供程序。Imports System .Data.SqlClient
公共类操作
Private ReadOnly _connectionString As String =
" Data Source = KARENS-PC; Initial Catalog = ForumExample;" &安培;
"Integrated Security = True"
私有_mStateTable As DataTable
Public ReadOnly属性StateTable As DataTable
获取
返回_mStateTable
结束获取
结束属性
私有_mCityTable As DataTable
Public ReadOnly Property CityTable As DataTable
Get
Return _mCityTable
End Get
End Property
'''< summary>
'''只加载一部分数据以保持
'''演示重量轻。
'''< / summary>
Private Sub LoadStatesCityTables()
_mStateTable = New DataTable
_mCityTable = New DataTable
使用cn作为新的SqlConnection使用{.ConnectionString = _connectionString}
使用cmd As New SqlCommand With {.Connection = cn}
cmd.CommandText =" SELECT TOP 10 Id,[Name] AS name" &安培;
" FROM dbo.StateMaster ORDER BY Name"
cn.Open()
_mStateTable.Load(cmd.ExecuteReader)
cmd.CommandText =
" SELECT ID, LTRIM(名称)AS名称,StateID" &安培;
" FROM dbo.CityMaster" &安培;
" WHERE StateId IN(3352,3119,3423,1433,269,2012,2013,3804,3805,1301)" &安培;
" ORDER BY Name"
_mCityTable.Load(cmd.ExecuteReader)
结束使用
结束使用
结束子
Public Sub New()
LoadStatesCityTables()
End Sub
End Class将城市ComboBox设置为自动完成的表单代码。
Public Class Form1
Private _cityTable As DataTable
Private Sub Form1_Load(sender As Object,e As EventArgs)_
Handles MyBase.Load
Dim ops As New Operations
cboStates.DataSource = ops.StateTable
cboStates.DisplayMember =" Name"
_cityTable = ops.CityTable
cboCity.DataSource = _cityTable
cboCity.DisplayMember =" Name"
cboCity.AutoCompleteMode = AutoCompleteMode.SuggestAppend
cboCity.AutoCompleteSource = AutoCompleteSource.CustomSource
AddHandler cboStates.SelectedIndexChanged,
AddressOf cboStates_SelectedIndexChanged
GetCities()
End Sub
Private Sub cboStates_SelectedIndexChanged(sender As Object,e As EventArgs)
GetCities()
End Sub
Private Sub GetCities()
Dim source As New AutoCompleteStringCollection
Dim stateId = CType(cboStates.SelectedItem,DataRowView)。
Row.Field(Of Integer)(" Id")
_cityTable.DefaultView.RowFilter =
" StateId = {stateId}" ;每个项目的
为DataRowView在_cityTable.DefaultView
source.Add(item(" Name")。)ToString())
Next
cboCity.AutoCompleteCustomSource = source
End Sub
End Class
Note: In Windows Based Application (VB.NET)
when user click on state combobox at that time city combobox is disabled and when user select state then it related city will be displayed in city combobox.
*****************************************************************************
in database i have two table named : State and City
field name for State : State_ID and State_Name
field name for City : City_ID, State_Id and City_Name
*****************************************************************************
Hello,
The following code sample (full source in vb.net) uses a database I found on the web a while back.
I wrote the following code
Classes to read data, note I have restricted data as the tables are very large to keep things light weight. Note the Data Source for the SQL-Server needs to change from KARENS-PC to your server name or if using Express edition of SQL-SERVER change to .\SQLEXPRESS. If using MS-Access the code logic is the same but you need to adjust to use OleDb rather than SqlClient data provider.
Imports System.Data.SqlClient Public Class Operations Private ReadOnly _connectionString As String = "Data Source=KARENS-PC;Initial Catalog=ForumExample;" & "Integrated Security=True" Private _mStateTable As DataTable Public ReadOnly Property StateTable As DataTable Get Return _mStateTable End Get End Property Private _mCityTable As DataTable Public ReadOnly Property CityTable As DataTable Get Return _mCityTable End Get End Property ''' <summary> ''' Only a subset of data is loaded to keep ''' the demo light weight. ''' </summary> Private Sub LoadStatesCityTables() _mStateTable = New DataTable _mCityTable = New DataTable Using cn As New SqlConnection With {.ConnectionString = _connectionString} Using cmd As New SqlCommand With {.Connection = cn} cmd.CommandText = "SELECT TOP 10 Id,[Name] AS name " & "FROM dbo.StateMaster ORDER BY Name" cn.Open() _mStateTable.Load(cmd.ExecuteReader) cmd.CommandText = "SELECT ID,LTRIM(Name) AS name,StateID " & "FROM dbo.CityMaster " & "WHERE StateId IN (3352,3119,3423,1433,269,2012,2013,3804,3805,1301) " & "ORDER BY Name" _mCityTable.Load(cmd.ExecuteReader) End Using End Using End Sub Public Sub New() LoadStatesCityTables() End Sub End ClassForm code where the city ComboBox is setup as auto-complete.
Public Class Form1 Private _cityTable As DataTable Private Sub Form1_Load(sender As Object, e As EventArgs) _ Handles MyBase.Load Dim ops As New Operations cboStates.DataSource = ops.StateTable cboStates.DisplayMember = "Name" _cityTable = ops.CityTable cboCity.DataSource = _cityTable cboCity.DisplayMember = "Name" cboCity.AutoCompleteMode = AutoCompleteMode.SuggestAppend cboCity.AutoCompleteSource = AutoCompleteSource.CustomSource AddHandler cboStates.SelectedIndexChanged, AddressOf cboStates_SelectedIndexChanged GetCities() End Sub Private Sub cboStates_SelectedIndexChanged(sender As Object, e As EventArgs) GetCities() End Sub Private Sub GetCities() Dim source As New AutoCompleteStringCollection Dim stateId = CType(cboStates.SelectedItem, DataRowView). Row.Field(Of Integer)("Id") _cityTable.DefaultView.RowFilter =
"StateId = {stateId}" for each item as DataRowView In _cityTable.DefaultView source.Add(item("Name").ToString()) Next cboCity.AutoCompleteCustomSource = source End Sub End Class
这篇关于如何将州与城市联系起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!