zoukankan      html  css  js  c++  java
  • 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;
            }
    
    
        }
        
    }
  • 相关阅读:
    JAVA线程池管理及分布式HADOOP调度框架搭建
    技术人员如何创业《二》 合伙人的模式
    JavaScript中的运动数学函数(持续更新)
    JavaScript中的加号
    JavaScript 函数绑定 Function.prototype.bind
    基于C#的波形显示控件的实现[附完整源码下载]
    JavaScript中的声明提升
    JavaScript & HTML5 Canvas 概览 更新时间201404111805
    《编写高质量代码——Web前端开发修炼之道》读后随笔
    B树/B+树/二叉搜索树/AVL树/红黑树
  • 原文地址:https://www.cnblogs.com/zhengqian/p/6825054.html
Copyright © 2011-2022 走看看