• 2017-5-8 TreeView 实现三级联动 (递归方法)


    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;
            }
    
    
        }
        
    }
  • 相关阅读:
    my sql Group_concat函数
    [Effective JavaScript 笔记]第43条:使用Object的直接实例构造轻量级的字典
    [Effective JavaScript 笔记]第4章:对象和原型--个人总结
    [Effective JavaScript 笔记]第42条:避免使用轻率的猴子补丁
    [Effective JavaScript 笔记]第41条:将原型视为实现细节
    [Effective JavaScript 笔记]第40条:避免继承标准类
    [Effective JavaScript 笔记]第39条:不要重用父类的属性名
    [Effective JavaScript 笔记]第38条:在子类的构造函数中调用父类的构造函数
    [Effective JavaScript 笔记]第37条:认识到this变量的隐式绑定问题
    [Effective JavaScript 笔记]第36条:只将实例状态存储在实例对象中
  • 原文地址:https://www.cnblogs.com/zhengqian/p/6825054.html
Copyright © 2020-2023  润新知