加载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);
                }
            }
        }

  1.         /// <summary>
  2.         /// 设置所有父节点为未勾选
  3.         /// </summary>
  4.         /// <param name="node"></param>
  5.         private void ClearFirstChecked()
  6.         {
  7.             foreach (TreeNode node in tvPower.Nodes)
  8.             {
  9.                 node.Checked = false;
  10.                 ClearSubChecked(node);
  11.             }
  12.         }
  13.         /// <summary>
  14.         /// 设置所有子节点为未勾选
  15.         /// </summary>
  16.         /// <param name="node"></param>
  17.         private void ClearSubChecked(TreeNode node)
  18.         {
  19.             if (node.Nodes != null)
  20.             {
  21.                 foreach (TreeNode subNode in node.Nodes)
  22.                 {
  23.                     subNode.Checked = false;
  24.                     ClearSubChecked(subNode);
  25.                 }
  26.             }
  27.         }
  28.         /// <summary>
  29.         /// 设置父结点Checked
  30.         /// </summary>
  31.         private void SetFirstChecked(string roleid)
  32.         {
  33.             //取消所有勾选项
  34.             ClearFirstChecked();
  35.             
  36.             prole_presource = new BLLPRole_PResource();
  37.             list= prole_presource.GetAll(roleid);
  38.             if (list != null && list.Count > 0)
  39.             {
  40.                 foreach (TreeNode node in this.tvPower.Nodes)
  41.                 {
  42.                     //此节点是否要被选中的中间变量
  43.                     //如果此节点在list中存在就设置为true
  44.                     bool isCheck = false;
  45.                     foreach (ModPRole_PResource mod in list)
  46.                     {
  47.                         if (node.Name == mod.Resourceid.ToString())
  48.                         {
  49.                             isCheck = true;
  50.                             SetSubChecked(node);
  51.                             break;
  52.                         }
  53.                     }
  54.                     node.Checked = isCheck;
  55.                 }
  56.                 
  57.                     
  58.             }            
  59.         }
  60.         /// <summary>
  61.         /// 设置子结点Checked
  62.         /// </summary>
  63.         private void SetSubChecked(TreeNode node)
  64.         {
  65.             if (node != null && node.Nodes.Count > 0)
  66.             {
  67.                 foreach (TreeNode subNode in node.Nodes)
  68.                 {
  69.                     //此节点是否要被选中的中间变量
  70.                     //如果此节点在list中存在就设置为true
  71.                     bool isCheck=false;
  72.                     foreach (ModPRole_PResource mod in list)
  73.                     {
  74.                         if (subNode.Name == mod.Resourceid.ToString())
  75.                         {
  76.                             isCheck = true;
  77.                             SetSubChecked(subNode);
  78.                             break;
  79.                         }                        
  80.                     }
  81.                     subNode.Checked = isCheck;
  82.                 }
  83.             }
  84.         }       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值