C#treeview树形菜单

/// 
        ///TrreView数据绑定
        /// 
        /// 获取datatable数据
        private void InitModuleTree(DataTable dt)
        {
            //清空treeview上所有记录
            this.TRole.Nodes.Clear();
            
            int[] father_node= new int[dt.Rows.Count];
            int[] child_node= new int[dt.Rows.Count];

            string account = Convert.ToString(user_Info.account);
            DataTable dtqx = slmenubll.GetData("*", "Library.dbo.czy_role_authority", "where account='" + account + "'");

            for (int i = 0; i < father_node.Length; i++)
            {
                //父级菜单
                string node= dt.Rows[i][2].ToString();
                //子集菜单
                int child= int.Parse(dt.Rows[i][2].ToString());
                for (int j = 0; j < dtqx.Rows.Count; j++)//权限设置
                {
                    int it = int.Parse(dtqx.Rows[j][1].ToString());
                    if (child== it)
                    {
                        if (node.Length > 1)   //表示是子节点   eg:1-2
                        {
                        	//当前已【-】为父级菜单与子集菜单之间的分割
                            father_node[i] = int.Parse(node.Substring(0, node.IndexOf('-')));
                            child_node[i] = int.Parse(node.Substring(node.IndexOf('-') + 1));
                        }
                        else 
                        {
                            //获取到所有父级菜单项
                            child_node[i] = int.Parse(node);
                            TreeNode nodeParent = new TreeNode();
                            nodeParent.Tag = (child_node[i]).ToString();
                            nodeParent.Text = dt.Rows[i][0].ToString();
                            TRole.Nodes.Add(nodeParent);
                        }
                        break;
                    }
                }
            }
            bindChildNote(dt, father_node, child_node);
        }
        /// 
        /// 绑定子级菜单
        /// 
        /// 
        /// 父级菜单
        /// 子集菜单
        private void bindChildNote(DataTable dt, int[] father_node, int[] child_node)
        {
            for (int i = 0; i < father_node.Length; i++)
            {
                if (father_node[i] != 0 && child_node[i] != 0)        //便利所有节点,找到所有子节点
                {
                    TreeNode childNode = new TreeNode();
                    foreach (TreeNode item in tree_Role.Nodes)   //便历treeview上所有父节点
                    {
                       //获取子级菜单上的父级菜单
                        if (item.Tag.ToString() == father_node[i].ToString())
                        {
                            childNode.Tag = child_node[i].ToString();
                            childNode.Text = dt.Rows[i][0].ToString();
                            item.Nodes.Add(childNode);
                        }
                    }
                }
            }
            //生成树形图
            TRole.ExpandAll();
        }

你可能感兴趣的:(C#treeview树形菜单)