C# TreeView選單,MenuStrip選單遞迴動態生成例子

iDotNetSpace發表於2009-04-15

這是在許可權系統中設計中常用到的兩種選單處理方式

    private DataTable ModuleTable;

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



    
/// 
        
/// 初始化選單
        
/// 
        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);

        }

        
/// 
        
/// 遞迴建立TreeView選單(模組列表)
        
/// 
        
/// 子選單
        
/// 子選單上級ID
        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);
                }
            }
        }

        
/// 
        
/// 遞迴建立MenuStrip選單(模組列表)
        
/// 
        
/// 父選單項
        
/// 父選單的ID
        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);
                }
            }
        }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-589269/,如需轉載,請註明出處,否則將追究法律責任。

相關文章