C# 递归实现tree view(树结构)
1.声明树结构model类
public class TreeModel{public string ID { set; get; }public string PARENTID { set; get; }public string NAME { set; get; }public List<TreeModel> TREECHILDREN { set; get; }}
2.利用反射将泛型list转换为树形结构类
/// <summary>/// 公用递归(反射转换List)/// </summary>/// <typeparam name="T"></typeparam>/// <param name="allList">数据列表</param>/// <param name="parentId">父级ID</param>/// <param name="idField">id字段名称</param>/// <param name="parentIdField">父级id字段名称</param>/// <param name="nameField">name字段名称</param>/// <returns></returns>public List<TreeModel> ConversionList<T>(List<T> allList, string parentId, string idField, string parentIdField, string nameField){List<TreeModel> list = new List<TreeModel>();TreeModel model = null;foreach (var item in allList){model = new TreeModel();foreach (System.Reflection.PropertyInfo p in item.GetType().GetProperties()){if (p.Name == idField){model.ID = p.GetValue(item)?.ToString();//判空}if (p.Name == parentIdField){model.PARENTID = p.GetValue(item)?.ToString();//判空}if (p.Name == nameField){model.NAME = p.GetValue(item)?.ToString();//判空}}list.Add(model);}return OperationParentData(list, parentId);}
3.递归
/// <summary>/// 公用递归(处理递归最父级数据)/// </summary>/// <param name="treeDataList">树形列表数据</param>/// <param name="parentId">父级Id</param>/// <returns></returns>public List<TreeModel> OperationParentData(List<TreeModel> treeDataList, string parentId){var data = treeDataList.Where(x => x.PARENTID == parentId);List<TreeModel> list = new List<TreeModel>();foreach (var item in data){OperationChildData(treeDataList, item);list.Add(item);}return list;}/// <summary>/// 公用递归(递归子级数据)/// </summary>/// <param name="treeDataList">树形列表数据</param>/// <param name="parentItem">父级model</param>public void OperationChildData(List<TreeModel> treeDataList, TreeModel parentItem){var subItems = treeDataList.Where(ee => ee.PARENTID == parentItem.ID).ToList();if (subItems.Count != 0){parentItem.TREECHILDREN = new List<TreeModel>();parentItem.TREECHILDREN.AddRange(subItems);foreach (var subItem in subItems){OperationChildData(treeDataList, subItem);}}}
/// <summary>/// 公用递归(递归子级数据)/// </summary>/// <param name="treeDataList">树形列表数据</param>/// <param name="parentItem">父级model</param>public void OperationChildData(List<TreeModel> treeDataList, TreeModel parentItem){var subItems = treeDataList.Where(ee => ee.PARENTID == parentItem.ID).ToList();if (subItems.Count != 0){parentItem.TREECHILDREN = new List<TreeModel>();parentItem.TREECHILDREN.AddRange(subItems);foreach (var subItem in subItems){OperationChildData(treeDataList, subItem);}}}
示例:
C# 递归实现tree view(树结构)相关推荐
- Angular实现递归指令 - Tree View
在层次数据结构展示中,树是一种极其常见的展现方式.比如系统中目录结构.企业组织结构.电子商务产品分类都是常见的树形结构数据. 这里我们采用Angular的方式来实现这类常见的tree view结构. ...
- JavaFX UI控件教程(十四)之Tree View
翻译自 Tree View 在本章中,您将学习如何在JavaFX应用程序中构建树结构,向树视图添加项,处理事件以及通过实现和应用单元工厂来自定义树单元. 包的TreeView类javafx.sce ...
- (转)用Ajax技术让IE Web Control Tree View实现大数据量读取
转自:http://www.cnblogs.com/dingsea/archive/2005/10/26/262220.html 相信不少人都用过微软提供的一款控件: IEControl, 其中的Tr ...
- USB HID 多设备检查工具,使用USB Tree View源码修改而来
最近一直在研究USB HID多设备升级,然后就查看了微软提供的WDK USB Tree View源码,但是没有找到USB多设备端口绑定的决定因素,当在电脑接上多个相同的USB设备时,就不好判断USB ...
- MPT(Merkle Patricia Tree)树结构示例
MPT(Merkle Patricia Tree): 参考深入浅出以太坊MPT(Merkle Patricia Tree) MPT树结构示例1: rootHash扩展节点 prefix 0 prefi ...
- linux tree命令 树结构显示目录文件
命令简介 tree命令的中文意思为"树",功能是以树形结构列出指定目录下的所有内容,包括所有文件.子目录及子目录里的目录和文件. 命令格式 tree [option] [direc ...
- tree view 後台編寫
因為我要與數據庫聯系起來,所以必須後台編寫: 'tree 綁定 Private Sub Loadtree()Sub Loadtree() Dim myda As SqlData ...
- visual tree view在工具箱力没有_Visual-Inertial Odometry
1. 定义 "Visual-Inertial Odometry",俗称VIO,是一个使用一个或者多个相机.一个或者多个IMU(Inertial Measurement Units) ...
- 简单的无限分级类 category, menus, jquery tree view
1. 简单的无限分级类 category, menus <?phpmysql_connect("localhost","root","root& ...
- tree view android,AndroidTreeView
特点 N-级 可展开与收缩的树 自定义值,view,节点样式 横竖屏切换可保持展开(或者关闭)状态 支持节点的选择模式 动态删除或者添加节点. 使用 添加依赖:compile 'com.github. ...
最新文章
- 怎么修改网页服务器数据库连接,如何修改网页服务器数据库连接
- 从脚本本身获取Bash脚本的源目录
- 利用NLTK进行分句分词
- python 全栈开发,Day66(web应用,http协议简介,web框架)
- 对南昌杀人案的一些看法
- Spring的异步方法
- 黑马程序员——11 正则表达式
- 小程序是否转发群还是个人(转发功能)
- 【[USACO13NOV]没有找零No Change】
- 推荐21个顶级的Vue UI库!
- Java连接mysql数据库的方式_java连接MySQL数据库的方式
- easyui框架和显示(一)
- Vue2在使用Element-UI的分页组件遇到的问题(如PageSize,PageNum等)
- 集合和数组区别_了解您的编码基础:集合和数组之间的主要区别
- 牵线搭桥,『桥接模式』
- 指甲之怎样以指甲测健康
- X-Powered-By: Servlet/3.0漏洞修复
- Python中10个常用的内置函数
- javascript程序员简历模板
- ArcGIS|一文彻底搞懂GIS图斑编号
热门文章
- 求大于3的素数c语言,c语言判断一个大于3的数是否是素数
- 3803. 数组去重-AcWing题库
- spark sql 入门详解
- 让程序员爱不释手的两个导航网站
- 牛客练习赛26---未完
- 乐学计算机,乐学高考电脑版
- C语言中使输入的字符串反序输出,C语言: 写一函数,使输入的一个字符串按反序存放,在主函数中输入和输出字符串。...
- er图一对多的关系怎么体现_抠图高级技法之混合剪切篇
- linux 解决Linux上解压jdk报错gzip: stdin: not in gzip format
- Redis-槽道原理