TreeView递归绑定无限分类数据
2015-06-17 00:06
495 查看
TreeView递归绑定无限分类数据
实现一个动态绑定,无限级分类数据时,需要将数据绑定到TreeView控件,分类表的结构是这样的:
实现数据绑定:
TreeViewBind
实现一个动态绑定,无限级分类数据时,需要将数据绑定到TreeView控件,分类表的结构是这样的:
字段 | 类型 |
Id | int |
ParentId | int |
Name | Nvarchar(64) |
private void ControlsDataBind() { tvCategory.Nodes.Clear(); List<Models.Category> types = CommonNews.Helper.OperateContext.Current.LoadNewsTypes(); IEnumerable<Models.Category> rootNodes = types.Where(t => t.ParentId == 0); TreeNode node = null; foreach (Models.Category item in rootNodes) { node = new TreeNode(item.CategoryName, item.CategoryId.ToString()); if (tvCategory.Nodes.Contains(node)) { continue; } IEnumerable<Models.Category> ts = types.Where(t => t.ParentId == item.CategoryId); AddNodesToTree(ts, node, 0); } } private void AddNodesToTree(IEnumerable<Models.Category> category, TreeNode node, int level) { TreeNode childNode = null; foreach (Models.Category c in category) { childNode = new TreeNode(c.CategoryName, c.CategoryId.ToString()); if (tvCategory.Nodes.Contains(childNode)) { continue; } node.ChildNodes.Add(childNode); AddNodesToTree(category.Where(t => t.CategoryId == c.ParentId), childNode, level + 1); } tvCategory.Nodes.Add(node); }
TreeViewBind
相关文章推荐
- protobuf repeated类型的使用
- SGU139 Help Needed!
- power_save模式
- 一个关于log4j的悲伤的故事
- vmware克隆linux虚拟机网卡无法上网的解决办法
- cocos2dx的内存管理机制
- poj 1679 判断MST是不是唯一的 (次小生成树)
- 连接阿里云服务器上的MySQL数据库
- 谷歌 Project Zero 团队宣布新政策,漏洞披露前将有完整的 90 天缓冲期
- JavaScript基础重点(必看)
- ASP.NET中使用Application对象实现简单在线人数统计功能
- .NET程序调试技巧(一):快速定位异常的一些方法
- webservice实例
- Python中map和列表推导效率比较实例分析
- Django中模型Model添加JSON类型字段的方法
- Python求两个文本文件以行为单位的交集、并集与差集的方法
- Python操作串口的方法
- Python合并两个字典的常用方法与效率比较
- Python使用urllib2模块实现断点续传下载的方法
- Python实现的使用telnet登陆聊天室实例