加载TreeView并设置复选框

        /// <summary>
        /// 加载父结点
        /// </summary>
        private void LoadFirstNodes()
        {
            BLLPResource resource = new BLLPResource();
            List<ModPResource> firstNodeList = resource.GetFirstNodes();
            if (firstNodeList != null && firstNodeList.Count > 0)
            {
                foreach (ModPResource r in firstNodeList)
                {
                    TreeNode treeNode = new TreeNode();
                    treeNode.Name = r.Resourceid.ToString();
                    treeNode.Text = r.MenuText;
                    LoadSubNodes(treeNode);
                    this.tvPower.Nodes.Add(treeNode);
                }
            }
        }

        /// <summary>
        /// 加载子结点
        /// </summary>
        /// <param name="node"></param>
        private void LoadSubNodes(TreeNode node)
        {
            if (node != null )
            {
                BLLPResource resource = new BLLPResource();
                string menuParent = node.Name;
                List<ModPResource> subNodeList = resource.GetSubNodes(menuParent);
                foreach (ModPResource r in subNodeList)
                {
                    TreeNode subNode = new TreeNode();
                    subNode.Text = r.MenuText;
                    subNode.Name = r.Resourceid.ToString();
                    LoadSubNodes(subNode);
                    node.Nodes.Add(subNode);
                }
            }
        }


        /// <summary>
        /// 设置所有父节点为未勾选
        /// </summary>
        /// <param name="node"></param>
        private void ClearFirstChecked()
        {
            foreach (TreeNode node in tvPower.Nodes)
            {
                node.Checked = false;
                ClearSubChecked(node);
            }
        }
        /// <summary>
        /// 设置所有子节点为未勾选
        /// </summary>
        /// <param name="node"></param>
        private void ClearSubChecked(TreeNode node)
        {
            if (node.Nodes != null)
            {
                foreach (TreeNode subNode in node.Nodes)
                {
                    subNode.Checked = false;
                    ClearSubChecked(subNode);
                }
            }
        }
        /// <summary>
        /// 设置父结点Checked
        /// </summary>
        private void SetFirstChecked(string roleid)
        {
            //取消所有勾选项
            ClearFirstChecked();
            
            prole_presource = new BLLPRole_PResource();
            list= prole_presource.GetAll(roleid);
            if (list != null && list.Count > 0)
            {
                foreach (TreeNode node in this.tvPower.Nodes)
                {
                    //此节点是否要被选中的中间变量
                    //如果此节点在list中存在就设置为true
                    bool isCheck = false;
                    foreach (ModPRole_PResource mod in list)
                    {
                        if (node.Name == mod.Resourceid.ToString())
                        {
                            isCheck = true;
                            SetSubChecked(node);
                            break;
                        }
                    }
                    node.Checked = isCheck;
                }
                
                    
            }            
        }
        /// <summary>
        /// 设置子结点Checked
        /// </summary>
        private void SetSubChecked(TreeNode node)
        {
            if (node != null && node.Nodes.Count > 0)
            {
                foreach (TreeNode subNode in node.Nodes)
                {
                    //此节点是否要被选中的中间变量
                    //如果此节点在list中存在就设置为true
                    bool isCheck=false;
                    foreach (ModPRole_PResource mod in list)
                    {
                        if (subNode.Name == mod.Resourceid.ToString())
                        {
                            isCheck = true;
                            SetSubChecked(subNode);
                            break;
                        }                        
                    }
                    subNode.Checked = isCheck;
                }

            }
        }      

From:http://blog.csdn.net/inc_root/archive/2008/11/13/3294551.aspx

posted @ 2009-07-29 22:23  邓维  阅读(330)  评论(0编辑  收藏  举报