三级联动
上一级选项变动,下一集选项也随之变动!
在窗体建立三个ComBox。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//数据类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WindowsFormsApplication3.App_Code { public class China { private string _AreaCode;//地区的代码 public string AreaCode { get { return _AreaCode; } set { _AreaCode = value; } } private string _AreaName;//地区的名字 public string AreaName { get { return _AreaName; } set { _AreaName = value; } } private string _ParentAreaCode;//他的上一级地区的代码 public string ParentAreaCode { get { return _ParentAreaCode; } set { _ParentAreaCode = value; } } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//数据访问类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace WindowsFormsApplication3.App_Code { public class ChinaData { SqlConnection cnn = null; SqlCommand cmd = null; public ChinaData() { cnn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123"); cmd = cnn.CreateCommand(); } public List<China> select(string code) { List<China> list = new List<China>(); cmd.CommandText = "select * from ChinaStates where ParentAreaCode=@a"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a",code); cnn.Open(); SqlDataReader ss = cmd.ExecuteReader(); while (ss.Read()) { China c = new China(); c.AreaCode = ss[0].ToString(); c.AreaName = ss[1].ToString(); c.ParentAreaCode = ss[2].ToString(); list.Add(c); } cnn.Close(); return list; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//form1里面 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WindowsFormsApplication3.App_Code; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); dong(comboBox1, "0001"); //comboBox1.DataSource = new ChinaData().select("0001"); //comboBox1.ValueMember = "AreaCode"; //comboBox1.DisplayMember = "AreaName"; dong(comboBox2,comboBox1.SelectedValue.ToString()); //comboBox2.DataSource = new ChinaData().select(comboBox1.SelectedValue.ToString()); //comboBox2.ValueMember = "AreaCode"; //comboBox2.DisplayMember = "AreaName"; dong(comboBox3, comboBox2.SelectedValue.ToString()); //comboBox3.DataSource = new ChinaData().select(comboBox2.SelectedValue.ToString()); //comboBox3.ValueMember = "AreaCode"; //comboBox3.DisplayMember = "AreaName"; } private void Form1_Load(object sender, EventArgs e) { } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { dong(comboBox2, comboBox1.SelectedValue.ToString()); //comboBox2.DataSource = new ChinaData().select(comboBox1.SelectedValue.ToString()); //comboBox2.ValueMember = "AreaCode"; //comboBox2.DisplayMember = "AreaName"; } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { dong(comboBox3, comboBox2.SelectedValue.ToString()); //comboBox3.DataSource = new ChinaData().select(comboBox2.SelectedValue.ToString()); //comboBox3.ValueMember = "AreaCode"; //comboBox3.DisplayMember = "AreaName"; } public void dong(ComboBox c, string code) { c.DataSource = new ChinaData().select(code); c.ValueMember = "AreaCode"; c.DisplayMember = "AreaName"; } } }