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 三级联动.App_code; namespace treeview { public partial class Form1 : Form { public Form1() { InitializeComponent(); } List<chinastate> list = new chinastatedata().select(); private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < 10;i++ ) { //TreeNode tn = new TreeNode(); //tn.Name =i.ToString(); //tn.Text = i.ToString(); //for (int j = 0; j < 10;j++ ) //{ // TreeNode tn1 = new TreeNode(); // tn1.Name = j.ToString(); // tn1.Text = j.ToString(); // for (int k = 0; k < 10;k++ ) // { // TreeNode tn2 = new TreeNode(); // tn2.Name = k.ToString(); // tn2.Text = k.ToString(); // tn1.Nodes.Add(tn2); // } // tn.Nodes.Add(tn1); //} //treeView1.Nodes.Add(tn); foreach(chinastate c in list) { if(c.ParentAreaCode=="0001") { //省级 TreeNode tn = new TreeNode(); tn.Name = c.AreaCode; tn.Text = c.AreaName; bind(tn); //foreach(chinastate cc in list) //{ // if(cc.ParentAreaCode==c.AreaCode) // { // TreeNode tn1=new TreeNode(); // tn1.Name=cc.AreaCode; // tn1.Text=cc.AreaName; // tn.Nodes.Add(tn1); // } //} treeView1.Nodes.Add(tn); } } } } //递归方法 public void bind(TreeNode tn) { foreach (chinastate c in list) { if (c.ParentAreaCode == tn.Name) { TreeNode tn1 = new TreeNode(); tn1.Text = c.AreaName; tn1.Name = c.AreaCode; bind(tn1); tn.Nodes.Add(tn1); } } } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { MessageBox.Show(treeView1.SelectedNode.Text+" | "+treeView1.SelectedNode.Name); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 三级联动.App_code { public class chinastate { 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; } } } }
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 三级联动.App_code { public class chinastatedata { SqlConnection conn=null; SqlCommand cmd=null; public chinastatedata() { conn = new SqlConnection("server=.;database=data0216;user=sa;pwd=123"); cmd = conn.CreateCommand(); } public List<chinastate> select(string pcode) { List<chinastate> clist = new List<chinastate>(); cmd.CommandText = "select * from ChinaStates where ParentAreaCode=@a"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a",pcode); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while(dr.Read()) { chinastate c = new chinastate(); c.AreaCode = dr[0].ToString(); c.AreaName = dr[1].ToString(); c.ParentAreaCode = dr[2].ToString(); clist.Add(c); } conn.Close(); return clist; } public List<chinastate> select() { List<chinastate> clist = new List<chinastate>(); cmd.CommandText = "select * from ChinaStates"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { chinastate c = new chinastate(); c.AreaCode = dr[0].ToString(); c.AreaName = dr[1].ToString(); c.ParentAreaCode = dr[2].ToString(); clist.Add(c); } conn.Close(); return clist; } } }