首页 > 代码库 > treeview递归加载

treeview递归加载

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 WindowsFormsApplication2;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<China> list = new ChinaData().Select();

        private void button1_Click(object sender, EventArgs e)
        {

            foreach (China c in list)
            {
                if (c.ParentAreaCode == "0001")
                {
                    //省级
                    TreeNode tn = new TreeNode();
                    tn.Name = c.AreaCode;
                    tn.Text = c.AreaName;

                    Bind(tn);

                    treeView1.Nodes.Add(tn);
                }
            }
        }

        public void Bind(TreeNode tn)
        {
            foreach (China 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);
        }




    }
}

实体类

 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; }
        }


    }

数据访问类

public class ChinaData
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;

        public ChinaData()
        {
            conn = new SqlConnection("server=.;database=Data0216;user=sa;pwd=123");
            cmd = conn.CreateCommand();
        }

        public List<China> Select(string pcode)
        {
            List<China> clist = new List<China>();
            cmd.CommandText = "select *from ChinaStates where ParentAreaCode = @a";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@a", pcode);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                China c = new China();
                c.AreaCode = dr[0].ToString();
                c.AreaName = dr[1].ToString();
                c.ParentAreaCode = dr[2].ToString();

                clist.Add(c);
            }
            conn.Close();
            return clist;
        }

        public List<China> Select()
        {
            List<China> clist = new List<China>();
            cmd.CommandText = "select *from ChinaStates";
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                China c = new China();
                c.AreaCode = dr[0].ToString();
                c.AreaName = dr[1].ToString();
                c.ParentAreaCode = dr[2].ToString();

                clist.Add(c);
            }
            conn.Close();
            return clist;
        }
    }

 

treeview递归加载