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

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中

上一篇:Android中的一些小知识


下一篇:C#集合