C# List构造TreeView

 今天根据任务做了从数据库中查询得到两个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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12639172/viewspace-571198/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中的TreeView控件可以通过拖拽节点的方式来实现节点的移动和排序。在TreeView控件上启用节点拖拽功能需要设置AllowDrop属性为true,然后通过处理控件的DragEnter、DragOver和DragDrop事件来实现节点的拖拽。 具体实现步骤如下: 1. 设置TreeView控件的AllowDrop属性为true,启用拖拽功能。 2. 处理TreeView控件的DragEnter事件,在该事件中设置允许拖拽的效果。 3. 处理TreeView控件的DragOver事件,在该事件中实现节点拖拽时的效果。 4. 处理TreeView控件的DragDrop事件,在该事件中实现节点拖拽完成后的效果。 以下是一个简单的实现代码示例: ``` private void treeView1_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } private void treeView1_DragOver(object sender, DragEventArgs e) { Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y)); TreeNode targetNode = treeView1.GetNodeAt(targetPoint); if (targetNode != null) { TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode)); if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode)) { targetNode.BackColor = Color.LightGreen; e.Effect = DragDropEffects.Move; } else { e.Effect = DragDropEffects.None; } } } private void treeView1_DragDrop(object sender, DragEventArgs e) { Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y)); TreeNode targetNode = treeView1.GetNodeAt(targetPoint); TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode)); if (targetNode != null && !draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode)) { draggedNode.Remove(); targetNode.Nodes.Add(draggedNode); targetNode.BackColor = Color.White; } } private bool ContainsNode(TreeNode node1, TreeNode node2) { if (node2.Parent == null) return false; if (node2.Parent.Equals(node1)) return true; return ContainsNode(node1, node2.Parent); } ``` 注意:以上代码仅提供参考,实际使用中还需要根据具体需求进行适当的调整和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值