C# TreeView菜单,MenuStrip菜单递归动态生成例子

这是在权限系统中设计中常用到的两种菜单处理方式

     private  DataTable ModuleTable;

 
private   void  FrmMain_Load( object  sender, EventArgs e)
        {
   
                
try
                {
                    InitMenu();
                }
                
catch
                {
                    MenuTree.Nodes.Clear();
                }



    
///   <summary>
        
///  初始化菜单
        
///   </summary>
         protected   void  InitMenu()
        {
            DataSet ds
=   new  DataSet();
            ds
= bll.GetModuleTypeList( "" );
            RGP.BLL.RGP_Modules bll 
=   new  RGP.BLL.RGP_Modules();
            ModuleTable 
=  ds.Tables[ 0 ];   // 取得所有数据得到DataTable 

            
// 加载TreeView菜单
            LoadNode(MenuTree.Nodes,  " 0 " );

            
// 加载MenuStrip菜单
            ToolStripMenuItem topMenu  =   new  ToolStripMenuItem();
            LoadSubMenu(
ref  topMenu,  0 );

        }

        
///   <summary>
        
///  递归创建TreeView菜单(模块列表)
        
///   </summary>
        
///   <param name="node"> 子菜单 </param>
        
///   <param name="MtID"> 子菜单上级ID </param>
         protected   void  LoadNode(TreeNodeCollection node,  string  MtID)
        {
            DataView dvList 
=   new  DataView(ModuleTable);
            dvList.RowFilter 
=   " ModuleTypeSuperiorID= "   +  MtID;   // 过滤父节点 
            TreeNode nodeTemp;
            
foreach  (DataRowView dv  in  dvList)
            {
                nodeTemp 
=   new  TreeNode();
                nodeTemp.Tag 
=  dv[ " ModuleTypeID " ].ToString()  +   " ,0 " ;
                nodeTemp.Text 
=  dv[ " ModuleTypeName " ].ToString();   // 节点名称                 
                node.Add(nodeTemp);   // 加入节点 
                
// nodeTemp.SelectAction = TreeNodeSelectAction.Expand;
                
// nodeTemp.Expanded = false;

                
this .LoadNode(nodeTemp.Nodes, nodeTemp.Tag.ToString().Split( ' , ' )[ 0 ]);   // 递归 

                
// 增加子模块分类
                RGP.BLL.RGP_Modules bll  =   new  RGP.BLL.RGP_Modules();
                DataSet Module 
=  bll.GetModuleList( " ModuleTypeID= "   +  dv[ " ModuleTypeID " ].ToString());
                
int  i  =   0 ;
                
foreach  (DataRow child_dr  in  Module.Tables[ 0 ].Rows)
                {
                    
if  ((UserBox.GetUserTable().IsLimit  ==   true
                            
||  (child_dr[ " ModuleDisabled " ].ToString().ToLower()  ==   " true "
                            
&&  UserHandle.ValidationModule( int .Parse(child_dr[ " ModuleID " ].ToString()), RGP_Tag.Browse)))
                            
&&  child_dr[ " IsMenu " ].ToString().ToLower()  ==   " true " )
                    {
                        TreeNode ChildNode 
=   new  TreeNode(child_dr[ " ModuleName " ].ToString());
                        ChildNode.Tag 
=  child_dr[ " ModuleID " ].ToString()  +   " ,1 " ;
                        ChildNode.Expand();
                        nodeTemp.Nodes.Add(ChildNode);
                        i
++ ;
                    }
                }

                
// 删除不必要的模块分类节点。
                 if  (i  ==   0 )
                {
                    MenuTree.Nodes.Remove(nodeTemp);
                }
            }
        }

        
///   <summary>
        
///  递归创建MenuStrip菜单(模块列表)
        
///   </summary>
        
///   <param name="topMenu"> 父菜单项 </param>
        
///   <param name="ItemID"> 父菜单的ID </param>
         private   void  LoadSubMenu( ref  ToolStripMenuItem topMenu,  int  MtID)
        {
            DataView dvList 
=   new  DataView(ModuleTable);
            
// 过滤出当前父菜单下在所有子菜单数据(仅为下一层的)
            dvList.RowFilter  =   " ModuleTypeSuperiorID= "   +  MtID.ToString();
            ToolStripMenuItem subMenu;
            
foreach  (DataRowView dv  in  dvList)
            {
                
// 创建子菜单项
                subMenu  =   new  ToolStripMenuItem();
                subMenu.Text 
=  dv[ " ModuleTypeName " ].ToString();
                
// 判断是否为顶级菜单
                 if  (MtID  ==   0 )
                {
                    
this .SysMainMenu.Items.Add(subMenu);
                }
                
else
                {
                    topMenu.DropDownItems.Add(subMenu);
                }

                
// 递归调用
                LoadSubMenu( ref  subMenu, Convert.ToInt32(dv[ " ModuleTypeID " ]));

                
// 增加子模块分类
                RGP.BLL.RGP_Modules bll  =   new  RGP.BLL.RGP_Modules();
                DataSet Module 
=  bll.GetModuleList( " ModuleTypeID= "   +  dv[ " ModuleTypeID " ].ToString());
                
int  i  =   0 ;
                
foreach  (DataRow child_dr  in  Module.Tables[ 0 ].Rows)
                {
                    
if  ((UserBox.GetUserTable().IsLimit  ==   true
                            
||  (child_dr[ " ModuleDisabled " ].ToString().ToLower()  ==   " true "
                            
&&  UserHandle.ValidationModule( int .Parse(child_dr[ " ModuleID " ].ToString()), RGP_Tag.Browse)))
                            
&&  child_dr[ " IsMenu " ].ToString().ToLower()  ==   " true " )
                    {
                        ToolStripMenuItem ChildMenu 
=   new  ToolStripMenuItem(child_dr[ " ModuleName " ].ToString());
                        
subMenu.DropDownItems.Add(ChildMenu);
                        i
++ ;
                    }
                }


                
// 删除不必要的模块分类节点。
                 if  (i  ==   0 )
                {
                    
this .SysMainMenu.Items.Remove(subMenu);
                }
            }
        }


作者:Neo.Yan
出处:http://www.cnblogs.com/Nick4/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
欢迎大家访问我的网站-RedGlove.NET

你可能感兴趣的:(treeview)