Winform---代码实现递归动态生成TreeView菜单
2021-02-19 14:16
标签:while sqlserver body 设置图 roo mod eric help splay 数据库菜单表: SQL脚本: 相关表测试数据: 相关实体类: 数据库查询(简易版): TreeView递归实现: 实现效果: 欢迎前来指教! Winform---代码实现递归动态生成TreeView菜单 标签:while sqlserver body 设置图 roo mod eric help splay 原文地址:https://www.cnblogs.com/EasonDongH/p/8316236.html 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
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
文章标题:Winform---代码实现递归动态生成TreeView菜单
文章链接:http://soscw.com/essay/57552.html