首页 > 其他分享 >Treeview

Treeview

时间:2024-01-30 09:01:01浏览次数:28  
标签:node Name MenuId RoleId Treeview Data 节点

using DBHelper;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Linq;
using System.Text;

namespace MES
{
public partial class RolePower : UIForm
{
MyDB DataConn = new DBHelper.MyDB();
RoleDAL dal = new RoleDAL();
public int RoleId = 0;


public RolePower(int Id)
{
InitializeComponent();
RoleId = Id;
}

public RolePower()
{
InitializeComponent();
}

 


private void btncancel_Click(object sender, EventArgs e)
{
this.Close();
}


/// <summary>
/// 确定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDetermine_Click(object sender, EventArgs e)
{
try
{
dal.nodeDelete(RoleId);
RoleMenuEntity Rolenode = new RoleMenuEntity();
foreach (TreeNode node in TreeViews.Nodes)
{
if (node.Checked)
{
Rolenode.MenuId = Convert.ToInt32(node.Name);
Rolenode.RoleId = RoleId;
int result = dal.RoleMenuAdd(Rolenode);
// 递归处理子节点
GetCheckedChildNodes(node, null);
}
else
{
TreeNode rootNode = node; // 获取根节点
List<TreeNodeData> allNodesData = GetAllNodesData(rootNode); // 获取所有节点数据
}
}
this.Close();
}
catch { }
}


private void GetCheckedChildNodes(TreeNode parentNode, TreeNode pNode)
{
try
{
RoleMenuButtonEntity Data = new RoleMenuButtonEntity();
List<TreeNode> uncheckedNodes = new List<TreeNode>();

foreach (TreeNode childNode in parentNode.Nodes)
{
if (childNode.Checked == true)
{
List<MenuEntity> list = dal.NodeExist(childNode.Name, childNode.Text);
if (list.Count > 0)
{
// 递归处理孙子节点(如果存在)
GetCheckedChildNodes(childNode, null);

// 递归处理孙子节点(如果存在)
GetCheckedChildNodes(childNode, null);
Data.MenuId = Convert.ToInt32(childNode.Name);
Data.ButtonId = Convert.ToInt32(childNode.Name);
Data.RoleId = RoleId;
//判断子节点是否保存过,避免重复保存数据
List<RoleMenuButtonEntity> Reult = dal.NodeExist(Data);
if (Reult.Count == 0)
{
//保存节点到数据库
int result = dal.MenuButtonAdd(Data);
}
}
else
{
TreeNode thirdLevelNode = childNode; // 获取或创建一个三级节点
if (thirdLevelNode.Parent != null) // 检查是否存在父节点
{
// 递归处理孙子节点(如果存在)
GetCheckedChildNodes(childNode, null);
Data.MenuId = Convert.ToInt32(thirdLevelNode.Parent.Name);
Data.ButtonId = Convert.ToInt32(childNode.Name);
Data.RoleId = RoleId;
//判断子节点是否保存过,避免重复保存数据
List<RoleMenuButtonEntity> Reult = dal.NodeExist(Data);
if (Reult.Count == 0)
{
//保存节点到数据库
int result = dal.MenuButtonAdd(Data);
}
}
}
}
}
}
catch (Exception ex) { Console.WriteLine(ex); }
}

public List<TreeNodeData> GetAllNodesData(TreeNode node)
{
RoleMenuButtonEntity Data = new RoleMenuButtonEntity();
List<TreeNodeData> nodesData = new List<TreeNodeData>();
if (node != null)
{
foreach (TreeNode child in node.Nodes)
{
nodesData.AddRange(GetAllNodesData(child)); // 递归调用,获取子节点数据
}
if (node.Checked == true)
{
Data.MenuId = Convert.ToInt32(node.Parent.Name);
Data.ButtonId = Convert.ToInt32(node.Name);
Data.RoleId = RoleId;
List<RoleMenuButtonEntity> Reult = dal.NodeExist(Data);
if (Reult.Count == 0)
{
// node.Checked=false 保存节点到数据库
int result = dal.MenuButtonAdd(Data);

//查询按钮的根节点
List<RoleMenuButtonEntity> Reult03= dal.NodeSALL(Data);
if (Reult03.Count > 0)
{
Data.MenuId = Convert.ToInt32(Reult03[0].ParentId);

List<RoleMenuButtonEntity> Reult04 = dal.NodeEXitSALL(Data);
if (Reult04.Count== 0)
{
int result03 = dal.MenuNodeAdd(Data);
}
}
}
}
}
return nodesData;
}

public class TreeNodeData
{
public TreeNode Node { get; set; }
public bool Checked { get; set; }
}


private void btncancel_Click_1(object sender, EventArgs e)
{
this.Close();
}

private void RolePower_Load(object sender, EventArgs e)
{
Treeview();
}

 

 

/// <summary>
/// 查询节点方法
/// </summary>
public void Treeview()
{
TreeViews.Nodes.Clear();//清除所有节点
//1.查询所有节点
string sql = string.Format(" select m.Id menuid, m.Name menuname, m.ParentId parentid, m.Icon menuicon, mb.ButtonId buttonid, b.Name buttonname,rmb.MenuId, ");
sql += string.Format(" b.Icon buttonicon,rmb.RoleId roleid,case when isnull(rmb.ButtonId , 0) = 0 then 'false' else 'true' end checked ");
sql += string.Format(" from tbMenu m left join tbMenuButton mb on m.Id=mb.MenuId ");
sql += string.Format(" left join tbButton b on mb.ButtonId=b.Id ");
sql += string.Format(" left join tbRoleMenuButton rmb on(mb.MenuId=rmb.MenuId and mb.ButtonId=rmb.ButtonId and rmb.RoleId ='{0}')", RoleId);
sql += string.Format(" order by m.ParentId,m.Sort,b.Sort");
DataTable dt = Functions.gettable(sql);

DataView dataView = new DataView(dt);
//distinct取不重复的子节点
DataTable dtDistinct = dataView.ToTable(true, new string[] { "menuname", "menuid", "parentid" });
CreateNode(dtDistinct, null, 0,dt);
}

//2.添加节点(递归)
private void CreateNode(DataTable dtDistinct, TreeNode pNode, int parentId, DataTable dt)
{
//1.获取数据
DataTable ds = dt;
//2.获取要创建的节点数据
DataRow[] rows = dtDistinct.Select("ParentId=" + parentId);
DataRow[] rowx = ds.Select("ParentId=" + parentId);
if (rows.Length > 0)
{

foreach (DataRow r in rows)
{
//3.新建子节点
TreeNode node = new TreeNode();
node.Name = r["menuid"].ToString();
node.Text = r["menuname"].ToString();


if (dt.Rows.Count > 0 && dt != null)
{
var buttons = dt.AsEnumerable().Where(p => Convert.ToString(p["MenuId"]) == node.Name);
if (buttons != null && buttons.Count() > 0)
{
List<String> lsButton = buttons.Select(p => Convert.ToString(p["ButtonId"])).ToList();
String sql = string.Format(" select b.*,mb.MenuId,case when isnull(rmb.ButtonId , 0) = 0 then 'false' else 'true' end checked");
sql += string.Format(" from tbMenu m left join tbMenuButton mb on m.Id=mb.MenuId ");
sql += string.Format(" left join tbButton b on mb.ButtonId=b.Id left join tbRoleMenuButton rmb on(mb.MenuId=rmb.MenuId and mb.ButtonId=rmb.ButtonId and rmb.RoleId ='{0}')",RoleId);
sql += string.Format(" where b.Id IN ('{0}') and mb.MenuId='{1}' order by m.ParentId,m.Sort,b.Sort", string.Join("','", lsButton), node.Name);
DataTable dtButton = Functions.gettable(sql);
if (dtButton != null && dtButton.Rows.Count > 0)
{
foreach (DataRow dr in dtButton.Rows)
{
TreeNode buttonNode = new TreeNode();
buttonNode.Name = dr["Id"].ToString();
buttonNode.Text = dr["Name"].ToString();
node.Nodes.Add(buttonNode);
if (Convert.ToBoolean(dr["checked"]) == true)
{
buttonNode.Checked = true;
}
else
{
buttonNode.Checked = false;
}
}
}
}
}


//4.直接添加到TreeView Nodes还是添加指定节点
if (pNode != null)
{
pNode.Nodes.Add(node);
}
else
{
TreeViews.Nodes.Add(node);
}


// 设置Checked属性
bool isNodeInDs = false;
foreach (DataRow dr in rowx)
{
if (dr["menuid"].ToString() == node.Name && Convert.ToBoolean(dr["checked"])==true)
{
isNodeInDs = true;
break;
}
}
node.Checked = isNodeInDs;

//5.判断当前节点下有没有子节点 递归
CreateNode(dtDistinct, node, int.Parse(node.Name), dt);
}
}
}

 

 

}
}

标签:node,Name,MenuId,RoleId,Treeview,Data,节点
From: https://www.cnblogs.com/linnew/p/17995725

相关文章

  • MFC TreeView 控件的基本认识
    ▲树控件OnInitDialog()里面的一些基础操作。BOOLCMFCApplication1Dlg::OnInitDialog(){CDialogEx::OnInitDialog();//将“关于...”菜单项添加到系统菜单中。//IDM_ABOUTBOX必须在系统命令范围内。ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTB......
  • C++ Qt开发:TableView与TreeView组件联动
    Qt是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍TableView与TreeView组件联动的常用方法及灵活运用。本章我们继续实现表格的联动效果,当读者点击T......
  • .net自带的树控件TreeView用法
    原文链接:https://blog.csdn.net/wenchm/article/details/133276828https://blog.csdn.net/xiaogongzhu001/article/details/131100371    TreeView控件(树控件)可以为用户显示节点层次结构,每个节点又可以包含子节点,包含子节点的节点叫父节点。就像在Windows操作系统的Wind......
  • 打工笔记----------------------------跨进程控制SysTreeView32树状图控件的问题
    跨进程控制SysTreeView32树状图控件的问题,啥也不说了,直接上代码:publicpartialclassForm1:Form{//定义常量publicconstintWM_LBUTTONDBLCLK=0x020B;//左键双击消息publicconstintWM_RBUTTONDOWN=0x0204;//右键按下消息......
  • QT-对于MVC中典型QTreeView简单使用参考记录
    //创建以ui文件中对应View为载体的model<-此处使用QStandardItemModel(比较常用)QStandardItemModel*model=newQStandardItemModel(ui->treeView);model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("国家")<<QStringLiteral("省份"......
  • python tkinter treeview 操作示例
    1.建立Treeviewfromtkinterimport*fromtkinter.ttkimport*root=Tk()#建立Treeviewcolumns=(('ID',50),('S_ID',50),('S_NAME',120),('B_NAME',120),('Date_Taken',100),......
  • python tkinter treeview 仿 excel表格
    代码:fromtkinterimportttkfromtkinterimport*root=Tk()#初始框的声明columns=("姓名","IP地址")treeview=ttk.Treeview(root,height=18,show="headings",columns=columns)#表格treeview.column("姓名",width=100,a......
  • WPF仿VS TreeView
    [TemplatePart(Name="PART_Content",Type=typeof(ToggleButton))][TemplatePart(Name="Expander",Type=typeof(Panel))]publicclassOTreeViewItem:TreeViewItem{Panel?partContent;ToggleButton?pa......
  • C# treeview 如何遍历MenuStrip的菜单
    需求分析: 数据菜单有四级菜单,需要在程序界面登录的时候遍历菜单的内容开发环境:VSC#winform步骤1:新建一个窗体步骤2:新建一个MenuStrip,并且定义内部的名称步骤3:新建一个Treeview步骤4:开始编程,定义一个参数函数: GetMenu(TreeView V,MenuStrip S)步骤5:编写参数函数的代码......
  • 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(5) -- 树列表
    在我们展示一些参考信息的时候,有所会用树形列表来展示结构信息,如对于有父子关系的多层级部门机构,以及一些常用如字典大类节点,也都可以利用树形列表的方式进行展示,本篇随笔介绍基于WPF的方式,使用TreeView来洗实现结构信息的展示,以及对它的菜单进行的设置、过滤查询等功能的实现逻辑......