zoukankan      html  css  js  c++  java
  • TreeView递归绑定无限分类数据

    TreeView递归绑定无限分类数据

    实现一个动态绑定,无限级分类数据时,需要将数据绑定到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
  • 相关阅读:
    day 6 敌机
    day 11 绘制轮廓
    day 10 形态学处理 膨胀
    day 5 飞机发射子弹 难点??
    激活Navicat?如何注册Navicat?
    Gradle DSL method found: ‘android()’错误
    腾讯sdk配置
    Android模拟器报"Failed To Allocate memory 8"错误的解决办法
    文件上传工具swfupload[转]
    35个jquery技巧[转]
  • 原文地址:https://www.cnblogs.com/weihanli/p/4582200.html
Copyright © 2011-2022 走看看