treeview递归加载

Posted 游称

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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递归加载的主要内容,如果未能解决你的问题,请参考以下文章

treeview递归加载

Winform---代码实现递归动态生成TreeView菜单

C# TreeView 控件的综合使用方法

treeview递归绑定的两种方法

Winforms treeview,递归检查子节点问题

如何在窗体初始化的时候就展开所有TreeView控件的所有节点