对有父子关系的层级数据项,递归实现绑定到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        }
posted @ 2008-02-29 23:53  BetaGeek™  阅读(987)  评论(0编辑  收藏  举报