C# List構造TreeView

來源:互聯網
上載者:User

 今天根據任務做了從資料庫中查詢得到兩個List,然後又通過List構造TreeView;

  1 資料庫中有兩張表:

      權限類別型表:類型ID  Function_TypeID <PK>

                       類型編碼 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<FunctionType> lstFunctionType = new FunctionTypeManager().GetAllFunctionType();
            List<SystemFunction> lstFunction = GetAllSystemFunction();

            FunctionTypeTreeInit(ref lstFunctionType, toTreeView);//構建權限類別型樹            
            FunctionNodeInit(ref lstFunction, toTreeView);//在權限類別型樹的基礎上構建權限物件
        }

       /// <summary>
        /// 實現對許可權分類TreeView的初始化
        /// </summary>
        /// <param name="lstFunctionType">獲得的許可權分類List</param>
        /// <param name="toTreeView">指定的一個目標TreeView</param>
        /// 作者;阮班波
        /// 日期;2009-03-16
        public  void  FunctionTypeTreeInit(ref List<FunctionType> 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<toTreeView.Nodes.Count;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();
                    }
                }
            }

        }
        /// <summary>
        /// 在指定的TreeView上增加許可權節點
        /// </summary>
        /// <param name="lstFunction">權限物件列表</param>
        /// <param name="toTreeView">指定的一個目標TreeView</param>
        private void FunctionNodeInit(ref List<SystemFunction> lstFunction,TreeView toTreeView)
        {
            TreeNode tmpNode = new TreeNode();
            foreach(SystemFunction objSystemFunction in lstFunction)
            {
                tmpNode = null;
                for(int i = 0;i<toTreeView.Nodes.Count;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();
                }
            }

        }
        
        /// <summary>
        /// 遞迴尋找父節點
        /// </summary>
        /// <param name="tnParent">指定一個根節點,然後遍曆它</param>
        /// <param name="strValue">所要尋找的節點的Name</param>
        /// 作者:阮班波
        /// 日期: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;
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.