Winform 中实现省市联动效果
1,省的ID,Name直接定义成类,实例化后添加的到ComBox(DropDrownList)中,DisplayMember设置为ProName
2,绑定后SelectedIndex=0,显示绑定的第一个值
3,Configure中设置连接字符串,ConfigurationManager 中得到字符串的值
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Configuration; namespace 省市联动 { public partial class 省市联动 : Form { public 省市联动() { InitializeComponent(); } private void 省市联动_Load(object sender, EventArgs e) { BindPro(); //显示第一个 Province cmbBoxPro.SelectedIndex = 0; } //省发生变化的时候的事件 private void cmbBoxPro_SelectedIndexChanged(object sender, EventArgs e) { ProIDSeled(); //显示第一个 City cmbCity.SelectedIndex = 0; } //获取所选取的省的ID protected void ProIDSeled() { // ProCity ProSeled = new ProCity(); ProSeled = (ProCity)cmbBoxPro.SelectedItem; //选中项是 PorCity 的实例 if (ProSeled != null) { int numProIDSeled = ProSeled.ProID; string strProName = ProSeled.ProName; //绑定之前先把数据清除 cmbCity.Items.Clear(); cmbCity.SelectedItem = null; BindCity(numProIDSeled); //市也相应变化 //MessageBox.Show(numProIDSeled.ToString()); } } //绑定省 protected void BindPro() { string strConn = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; using (SqlConnection conn = new SqlConnection(strConn)) { conn.Open(); string strSelPro = "SELECT proID, proName FROM Province"; using (SqlCommand sqlCmd = new SqlCommand(strSelPro, conn)) { using (SqlDataReader sdr = sqlCmd.ExecuteReader()) { while (sdr.Read()) { ProCity PCity = new ProCity(); int numProID = sdr.GetInt32(sdr.GetOrdinal("ProID")); string strProName = sdr.GetValue(sdr.GetOrdinal("ProName")).ToString(); PCity.ProID = numProID; PCity.ProName = strProName; cmbBoxPro.Items.Add(PCity); //控件添加的是类 } } } } } //绑定市 protected void BindCity(int proID) { string strConn = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; using (SqlConnection conn = new SqlConnection(strConn)) { conn.Open(); string strSelPro = "SELECT cityID, cityName, proID FROM city where proID=@proId "; SqlParameter sqlParaCity = new SqlParameter("proId", proID); using (SqlCommand sqlCmd = new SqlCommand(strSelPro, conn)) { sqlCmd.Parameters.Add(sqlParaCity); using (SqlDataReader sdr = sqlCmd.ExecuteReader()) { while (sdr.Read()) { int numCityID = sdr.GetInt32(sdr.GetOrdinal("cityID")); string strCityName = sdr.GetValue(sdr.GetOrdinal("cityName")).ToString(); cmbCity.Items.Add(strCityName); //控件添加的是类 //参数清除 sqlCmd.Parameters.Clear(); } } } } } //定义一个省市的类 public class ProCity { private int proID; private string proName; public int ProID { get { return proID; } set { this.proID = value; } } public string ProName { get { return this.proName; } set { this.proName = value; } } } } }
配置文件中的字符串 App.Config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="ConnStr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DBProvince.mdf;Integrated Security=True;User Instance=True"/> </connectionStrings> </configuration>