ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C# List构造TreeView

C# List构造TreeView

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-04-07 15:20:45 0 删除 编辑

 今天根据任务做了从数据库中查询得到两个List,然后又通过List构造TreeView;

  1 数据库中有两张表:

      权限类型表:类型ID  Function_TypeID


                       类型编码 Function_TypeCode

                       类型名称 Function_TypeName

                       类型的父ID Function_TypeParentID

    权限信息表:权限ID Function_ID

                     权限编码:Function_Code

                     权限名称:Function_Name

                     权限类型ID: Function_TypeID

                     权限描述:Function_Describe

  2要根据权限类型表中获得权限类型List构造权限类型树,在权限类型树的基础上再根据从权限信息表中获得的权限List构造权限节点:

源代码
namespace MES.Business.UserPermission
{
    public class SystemFunctionManager
    {
        public void FunctionTreeInit(TreeView toTreeView)
        {
            List lstFunctionType = new FunctionTypeManager().GetAllFunctionType();
            List lstFunction = GetAllSystemFunction();

            FunctionTypeTreeInit(ref lstFunctionType, toTreeView);//构建权限类型树           
            FunctionNodeInit(ref lstFunction, toTreeView);//在权限类型树的基础上构建权限对象
        }

       ///


        /// 实现对权限分类TreeView的初始化
        ///

        /// 获得的权限分类List
        /// 指定的一个目标TreeView
        /// 作者;阮班波
        /// 日期;2009-03-16
        public  void  FunctionTypeTreeInit(ref List lstFunctionType ,TreeView toTreeView)
        {
            toTreeView.Nodes.Clear();
            TreeNode tmpNode = new TreeNode();
            foreach(FunctionType objFunctionType in lstFunctionType)
            {
                if(objFunctionType.Function_TypeParentID == null)
                {
                    TreeNode rootNode = new TreeNode ();
                    rootNode.Name =objFunctionType.Function_TypeID.ToString();
                    rootNode.Text = objFunctionType.Function_TypeName;
                    rootNode.Tag = objFunctionType.Function_TypeCode;                   
                    toTreeView.Nodes.Add(rootNode);
                    rootNode.Expand();
                 
                }
                else
                {
                    tmpNode = null;
                    for(int i = 0;i                    {
                        TreeNode ttNode = new TreeNode();
                        ttNode = FindNode(toTreeView.Nodes[i], objFunctionType.Function_TypeParentID.ToString().Trim());
                        if(ttNode!=null) tmpNode = ttNode;
                    }
                    if(tmpNode!=null)
                    {
                        TreeNode subNode = new TreeNode();
                        subNode.Text = objFunctionType.Function_TypeName;
                        subNode.Name = objFunctionType.Function_TypeID.ToString();
                        subNode.Tag = objFunctionType.Function_TypeCode;
                        tmpNode.Nodes.Add(subNode);
                        subNode.Expand();
                    }
                }
            }

        }
        ///


        /// 在指定的TreeView上增加权限节点
        ///

        /// 权限对象列表
        /// 指定的一个目标TreeView
        private void FunctionNodeInit(ref List lstFunction,TreeView toTreeView)
        {
            TreeNode tmpNode = new TreeNode();
            foreach(SystemFunction objSystemFunction in lstFunction)
            {
                tmpNode = null;
                for(int i = 0;i                {
                    TreeNode ttNode = new TreeNode();
                    ttNode = FindNode(toTreeView.Nodes[i],objSystemFunction.Function_TypeID.ToString().Trim());
                    if (ttNode != null) tmpNode = ttNode;
                }
                if (tmpNode != null)
                {
                    TreeNode subNode = new TreeNode();
                    subNode.Name = objSystemFunction.FunctionCode;
                    subNode.Text = objSystemFunction.FunctionName;
                    tmpNode.Nodes.Add(subNode);
                    subNode.Expand();
                }
            }

        }
       
        ///


        /// 递归查找父节点
        ///

        /// 指定一个根节点,然后遍历它
        /// 所要查找的节点的Name
        /// 作者:阮班波
        /// 日期:2009-03-16
        private  TreeNode FindNode(TreeNode tnParent, string strValue)
        {
            if (tnParent == null) return null;
            if (tnParent.Name == strValue) return tnParent;
            TreeNode tnRet = null;
            foreach (TreeNode tn in tnParent.Nodes)
            {
                tnRet = FindNode(tn, strValue);
                if (tnRet != null) break;
            }
            return tnRet;
        }
    }
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-571198/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5346190