码迷,mamicode.com
首页 > Windows程序 > 详细

C# TreeView,递归循环数据加载到treeView1中

时间:2014-10-29 21:17:42      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   for   sp   数据   

TblAreaBLL bll = new TblAreaBLL();
        private void button1_Click(object sender, EventArgs e)
        {
            LoadDataToTree(this.treeView1.Nodes, bll.GetTblAreaListByID(0));
        }

        private void LoadDataToTree(TreeNodeCollection treeNode, List<TblArea> list)
        {
            foreach (TblArea arr in list)
            {
                TreeNode tree = treeNode.Add(arr.AreaName);
                tree.Tag = arr.AreaId;
                LoadDataToTree(tree.Nodes, bll.GetTblAreaListByID(arr.AreaId)); //递归循环
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<TblArea> list = bll.GetTblAreaList(); //获取所有的城市升级列表
            LoadDataToTree2(this.treeView1.Nodes, list, 0); //先加载所有省级列表
        }

        private void LoadDataToTree2(TreeNodeCollection treeNode, List<TblArea> list, int pid)
        {
            foreach (TblArea arr in list)
            {
                if (arr.AreaPId == pid)
                {
                    TreeNode tree = treeNode.Add(arr.AreaName);
                    tree.Tag = arr.AreaId;
                    LoadDataToTree2(tree.Nodes, list, arr.AreaId); //递归循环  直接在集合中查询。
                }
            }
        }
        //删除节点下的所有数据
        private void button3_Click(object sender, EventArgs e)
        {
            TreeNode node = this.treeView1.SelectedNode;
            if (node != null)
            {
                int areaId = (int)node.Tag;
                bll.DeleteTblAreaDiGui(areaId);
                this.treeView1.SelectedNode.Remove();
                MessageBox.Show("删除成功!");
            }
        }

 

C# TreeView,递归循环数据加载到treeView1中

标签:des   style   blog   io   color   ar   for   sp   数据   

原文地址:http://www.cnblogs.com/han1982/p/4060440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!