C# List構造TreeView

iDotNetSpace發表於2009-04-07

 今天根據任務做了從資料庫中查詢得到兩個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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章