对有父子关系的层级数据项,递归实现绑定到ListControl进行树状列表化的分层输出...

数据库里有Category表,字段CategoryID,CategoryName,ParentID
也就是说,能够根据ParentID的存在而实现无限级分类.
约定CategoryID=0为最上层根目录
输出样式 以及在数据库里的结构


输出的类型为List<Category>, ListControl(比如ListBox,DropDownlist之类的控件)指定了TextField/ValueField后可以进行直接绑定
以ListBox为例:
            CateList.DataSource = categoryHandler.BindToList(false);
            CateList.DataTextField = "CategoryName";
            CateList.DataValueField = "CategoryID";
            CateList.DataBind();
            CateList.Rows = CateList.Items.Count;

 1
 2          /**/ /// <summary>
 3        /// 输出列表形式作为DropdownList或者Listbox的内容 
 4        /// </summary>
 5        /// <param name="NeedRoot">输出的List里是否要包含虚拟的最上层根目录</param>
 6        /// <returns></returns>

 7          public  List  < Category >  BindToList( bool  NeedRoot)
 8          {
 9
10            List<Category> list = new List<Category>();
11            Category rootCate = new Category { CategoryID = 0, CategoryName = "RootCate" };
12            list.Add(rootCate);
13            list = GetCateTree(rootCate, list, 0);
14            if (NeedRoot)
15                return list;
16            else
17            {
18                list.Remove(rootCate);
19                return list;
20            }

21         }

22
23          /**/ /// <summary>
24        /// 递归实现输出List
25        /// </summary>
26        /// <param name="cate">入口Cate</param>
27        /// <param name="list">要处理的List</param>
28        /// <param name="level">入口Cate的级数</param>
29        /// <returns></returns>

30          private  List < Category >  GetCateTree(Category  cate, List < Category >  list ,  int  level)
31         
32            foreach (Category childCate in GetChildCategoryList(cate))
33            {
34                childCate.CategoryName = RenameForList(childCate.CategoryName, level);
35                list.Insert(list.IndexOf(cate)+1, childCate);
36                if (GetChildCategoryList(childCate).Count() > 0)
37                    list = GetCateTree(childCate, list, level + 1);
38            }

39            return list;            
40        }

41
42          /**/ /// <summary>
43        /// 重命名CateName,实现树状显示样式
44        /// </summary>
45        /// <param name="name"></param>
46        /// <param name="level"></param>
47        /// <returns></returns>

48          private   string  RenameForList( string  name,  int  level)
49          {
50            string space = "";
51            if (level != 0)
52                space = "|--";
53            for (int i = 0; i < level; i++)
54            {
55                space = " "+ space ;//全角空格 半角绑定到ListControl会被自动Trim
56            }

57            return space + name;
58        }

59
60          /**/ /// <summary>
61        /// 取得所有子Cate的列表
62        /// </summary>
63        /// <param name="cate"></param>
64        /// <returns></returns>

65          private  IEnumerable < Category >  GetChildCategoryList(Category cate)
66          {
67            var childList = from c in db.Categories
68                            where c.ParentID == cate.CategoryID 
69                            select c;
70            return childList;
71        }

转载于:https://www.cnblogs.com/BetaGeek/archive/2008/02/29/1086765.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值