Winform---代码实现递归动态生成TreeView菜单
Posted EasonDongH
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Winform---代码实现递归动态生成TreeView菜单相关的知识,希望对你有一定的参考价值。
数据库菜单表:
SQL脚本:
1 use master 2 go 3 4 create database TreeViewDemo 5 on primary 6 ( 7 name=\'TreeViewDemo_data\', 8 filename=\'C:\\2Study\\SQLServer\\TreeViewDemo\\TreeViewDemo_data.mdf\', 9 size=5MB, 10 filegrowth=1MB 11 ) 12 13 create table MenuList 14 ( 15 MenuId int identity(1,1) primary key, 16 MenuName varchar(20) ,--菜单名称 17 MenuCode varchar(20),--菜单编号 18 ParentId int --父类编号 19 ) 20 21 --一级菜单 22 insert into MenuList (MenuName, MenuCode, ParentId) values (\'系统管理\',\'\',0)--1 23 insert into MenuList (MenuName, MenuCode, ParentId) values (\'学员管理\',\'\',0)--2 24 insert into MenuList (MenuName, MenuCode, ParentId) values (\'成绩管理\',\'\',0)--3 25 insert into MenuList (MenuName, MenuCode, ParentId) values (\'考勤管理\',\'\',0)--4 26 insert into MenuList (MenuName, MenuCode, ParentId) values (\'系统帮助\',\'Help\',0)--5 27 28 --二级菜单 29 insert into MenuList (MenuName, MenuCode, ParentId) values (\'密码修改\',\'ModifyPwd\',1) 30 31 insert into MenuList (MenuName, MenuCode, ParentId) values (\'添加学员\',\'AddStu\',2) 32 insert into MenuList (MenuName, MenuCode, ParentId) values (\'批量导入学员\',\'ImportStu\',2) 33 insert into MenuList (MenuName, MenuCode, ParentId) values (\'学员信息管理\',\'StuManage\',2) 34 35 insert into MenuList (MenuName, MenuCode, ParentId) values (\'成绩查询与分析\',\'ScoreQuery\',3) 36 insert into MenuList (MenuName, MenuCode, ParentId) values (\'成绩快速查询\',\'ScoreQuickQuery\',3) 37 38 insert into MenuList (MenuName, MenuCode, ParentId) values (\'考勤打卡\',\'ModifyPwd\',4) 39 insert into MenuList (MenuName, MenuCode, ParentId) values (\'考勤查询\',\'ModifyPwd\',4) 40 41 select * from MenuList
相关表测试数据:
相关实体类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TreeViewDemo { public class TVNode { public int MenuId { get; set; } public string MenuName { get; set; } public string MenuCode { get; set; } public int ParentId { get; set; } } }
数据库查询(简易版):
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Data.SqlClient; 7 8 namespace TreeViewDemo 9 { 10 public class SQLHelper 11 { 12 public List<TVNode> GetMenuList() 13 { 14 string connString = "Server=.;DataBase=TreeViewDemo;Uid=sa;Pwd=warrenwell"; 15 string sql = "select MenuId,MenuName, MenuCode, ParentId from MenuList"; 16 List<TVNode> objMenuList = new List<TVNode>(); 17 SqlConnection conn = new SqlConnection(connString); 18 SqlCommand cmd = new SqlCommand(sql,conn); 19 conn.Open(); 20 SqlDataReader objReader = cmd.ExecuteReader(); 21 while (objReader.Read()) 22 { 23 objMenuList.Add(new TVNode 24 { 25 MenuId=Convert.ToInt32(objReader["MenuId"]), 26 MenuName=objReader["MenuName"].ToString(), 27 MenuCode=objReader["MenuCode"].ToString(), 28 ParentId= Convert.ToInt32(objReader["ParentId"]) 29 }); 30 } 31 objReader.Close(); 32 conn.Close(); 33 return objMenuList; 34 } 35 } 36 }
TreeView递归实现:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Reflection; 11 12 namespace TreeViewDemo 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 private void Form1_Load(object sender, EventArgs e) 22 { 23 this.nodeList = new SQLHelper().GetMenuList(); 24 25 //创建根节点 26 this.tv.Nodes.Clear();//清空节点 27 TreeNode rootNode = new TreeNode(); 28 rootNode.Text = "学员管理系统"; 29 rootNode.Tag = 0; 30 rootNode.ImageIndex = 0; 31 this.tv.Nodes.Add(rootNode); 32 33 CreateChildNode(rootNode,0); 34 35 //this.tv.Nodes[0].Expand();//展开一级菜单 36 this.tv.ExpandAll();//展开所有菜单 37 } 38 39 private List<TVNode> nodeList = new List<TVNode>(); 40 private void CreateChildNode(System.Windows.Forms.TreeNode parentNode,int parentId) 41 { 42 //找到该节点下的子项(父节点值等于该节点编号) 43 var nodes = from list in this.nodeList 44 where list.ParentId.Equals(parentId) 45 select list; 46 //创建该节点子节点 47 foreach (var item in nodes) 48 { 49 TreeNode node = new TreeNode(); 50 node.Text = item.MenuName; 51 node.Tag = item.MenuCode; 52 53 ////此处可根据节点的parentId来设置图标 54 if (item.ParentId == 0) 55 { 56 node.ImageIndex = 1; 57 } 58 else 59 { 60 node.ImageIndex = 2; 61 } 62 //父节点添加子节点 63 parentNode.Nodes.Add(node); 64 //调用自身:递归 65 CreateChildNode(node,item.MenuId); 66 } 67 } 68 69 private void tv_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) 70 { 71 if (e.Node.Level == 2) 72 { 73 Form obj = (Form)Assembly.Load("TreeViewDemo").CreateInstance("TreeViewDemo.Frm" + e.Node.Tag.ToString()); 74 obj.Show(); 75 } 76 } 77 78 private void tv_AfterExpand(object sender, TreeViewEventArgs e) 79 { 80 e.Node.ImageIndex = 0; 81 } 82 83 private void tv_AfterCollapse(object sender, TreeViewEventArgs e) 84 { 85 e.Node.ImageIndex = 1; 86 } 87 } 88 }
实现效果:
欢迎前来指教!
以上是关于Winform---代码实现递归动态生成TreeView菜单的主要内容,如果未能解决你的问题,请参考以下文章