• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
丶白
博客园    首页    新随笔    联系   管理    订阅  订阅

WinForm TreeView递归加载

这个其实通俗一点讲就是的树状分支图

首先利用递归添加数据

数据放入 treeView1.Nodes.Add() 中

 public Form3()
        {
            InitializeComponent();

            TreeNode t1 = new TreeNode("中国");

            TreeNode t2 = new TreeNode("北京");

            TreeNode t3 = new TreeNode("朝阳区");

            t2.Nodes.Add(t3);

            t1.Nodes.Add(t2);

            treeView1.Nodes.Add(t1);
        }

然后再用tag 与对象关联的用户定义数据

public partial class Form3 : Form
    {
        List<China> alllist = new List<China>();

        public Form3()
        {
            InitializeComponent();

            alllist = new ChinaData().Select();

            TreeNode tn1 = new TreeNode("中国");
            tn1.Tag = "0001";

            NodesBind(tn1);

            treeView1.Nodes.Add(tn1);
            
        }

        public void NodesBind(TreeNode tn)
        {
            List<China> clist = alllist.Where(r => r.ParentAreaCode == tn.Tag.ToString()).ToList();

            foreach (China c in clist)
            {
                TreeNode tnn = new TreeNode(c.AreaName);
                tnn.Tag = c.AreaCode;

                //递归
                NodesBind(tnn);

                tn.Nodes.Add(tnn);
            }
        }

    }

 

posted @ 2016-12-16 21:05  丶白  阅读(3339)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3