开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 6541|回复: 2

递归调用 动态绑定treelist控件

[复制链接]

0

精华

105

贡献

62

赞扬

帖子
104
软币
876
在线时间
101 小时
注册时间
2013-8-21
发表于 2014-8-14 09:48:39 | 显示全部楼层 |阅读模式
本帖最后由 贾林朋 于 2014-8-14 09:48 编辑

本文以例子的形式介绍如何通过递归动态的生成树状结构,填充树,递归调用中传递的参数可以根据实际的需求改变
并不一定是intl 类型,写的不好欢迎大神批评指正
第一步定义绑定树结构用的类
[C#] 纯文本查看 复制代码
public class Employee
    {
        public int IDS { get; set; }
        public int ParentIDS { get; set; }
        public string Name { get; set; }
        public string Position { get; set; }
        public string Department { get; set; }
        public int Age { get; set; }
        public override string ToString()
        {
            return string.Format("姓名:{0}",Name);
        }
    }


第二部创建绑定树结构的数据源

[C#] 纯文本查看 复制代码
public static class Stuff
    {
        public static List<Employee> GetStuff()
        {
            List<Employee> stuff = new List<Employee>();
            stuff.Add(new Employee() { IDS = 1, ParentIDS = 0, Name = "Gregory S. Price", Department = "", Position = "President", Age = 57 });
            stuff.Add(new Employee() { IDS = 2, ParentIDS = 1, Name = "Irma R. Marshall", Department = "Marketing", Position = "Vice President", Age = 45 });
            stuff.Add(new Employee() { IDS = 3, ParentIDS = 1, Name = "John C. Powell", Department = "Operations", Position = "Vice President", Age = 43 });
            stuff.Add(new Employee() { IDS = 4, ParentIDS = 1, Name = "Christian P. Laclair", Department = "Production", Position = "Vice President", Age = 38 });
            stuff.Add(new Employee() { IDS = 5, ParentIDS = 1, Name = "Karen J. Kelly", Department = "Finance", Position = "Vice President", Age = 55 });

            stuff.Add(new Employee() { IDS = 6, ParentIDS = 2, Name = "Brian C. Cowling", Department = "Marketing", Position = "Manager", Age = 35 });
            stuff.Add(new Employee() { IDS = 7, ParentIDS = 2, Name = "Thomas C. Dawson", Department = "Marketing", Position = "Manager", Age = 40 });
            stuff.Add(new Employee() { IDS = 8, ParentIDS = 2, Name = "Angel M. Wilson", Department = "Marketing", Position = "Manager", Age = 32 });
            stuff.Add(new Employee() { IDS = 9, ParentIDS = 2, Name = "Bryan R. Henderson", Department = "Marketing", Position = "Manager", Age = 28 });

            stuff.Add(new Employee() { IDS = 10, ParentIDS = 3, Name = "Harold S. Brandes", Department = "Operations", Position = "Manager", Age = 27 });
            stuff.Add(new Employee() { IDS = 11, ParentIDS = 3, Name = "Michael S. Blevins", Department = "Operations", Position = "Manager", Age = 31 });
            stuff.Add(new Employee() { IDS = 12, ParentIDS = 3, Name = "Jan K. Sisk", Department = "Operations", Position = "Manager", Age = 44 });
            stuff.Add(new Employee() { IDS = 13, ParentIDS = 3, Name = "Sidney L. Holder", Department = "Operations", Position = "Manager", Age = 24 });

            stuff.Add(new Employee() { IDS = 14, ParentIDS = 4, Name = "James L. Kelsey", Department = "Production", Position = "Manager", Age = 27 });
            stuff.Add(new Employee() { IDS = 15, ParentIDS = 4, Name = "Howard M. Carpenter", Department = "Production", Position = "Manager", Age = 33 });
            stuff.Add(new Employee() { IDS = 16, ParentIDS = 4, Name = "Jennifer T. Tapia", Department = "Production", Position = "Manager", Age = 22 });

            stuff.Add(new Employee() { IDS = 17, ParentIDS = 5, Name = "Judith P. Underhill", Department = "Finance", Position = "Manager", Age = 41 });
            stuff.Add(new Employee() { IDS = 18, ParentIDS = 5, Name = "Russell E. Belton", Department = "Finance", Position = "Manager", Age = 36 });

            stuff.Add(new Employee() { IDS = 19, ParentIDS = 6, Name = "Russell E. Belton", Department = "Finance", Position = "Manager", Age = 36 });
            return stuff;
        }
    }


第三部绑定树状结构

[C#] 纯文本查看 复制代码
     #region TreeList 多节点绑定
        /// <summary>
         /// treeList绑定
         /// </summary>
         /// <param name="parent">父ID</param>
         private void TreeListBind(int parent)
         {
             treeList1.Nodes.Clear();
             if (collection.Count < 1)
                 return;
             var items = from s in collection where s.ParentIDS == parent
                         select s;
             if (items.Count() < 1)
                 return;
             foreach (var st in items)
             {
                 TreeListNode tn = treeList1.AppendNode(st.IDS, null);
                 tn.SetValue(ptreecolumn, st.Name);
                 tn.Tag = st; //目的:为了方便的得到对应的类的实例,可以更具需要动态的改变
                 GetCentralChild(tn, st.IDS);
             }
             treeList1.ExpandAll();
         }
 
         private void GetCentralChild(TreeListNode tn, int parent)
         {
             var items = from s in collection   where s.ParentIDS == parent
                         select s;
             if (items.Count() < 1)
                 return;
             foreach (var st in items)
             {
                 TreeListNode tns = tn.TreeList.AppendNode(st.IDS, tn);
                 tns.SetValue(ptreecolumn, st.Name);
                 tns.Tag = st;
                 GetCentralChild(tns, st.IDS);   
             }
         }
        #endregion


调用方法
窗体中声明
[C#] 纯文本查看 复制代码
 List<Employee> collection;
        TreeListColumn ptreecolumn = new TreeListColumn();
构造函数内初始化ptreecolumn对象
[C#] 纯文本查看 复制代码
 ptreecolumn.Name = "hello";
            ptreecolumn.FieldName = "你好";
            ptreecolumn.Caption = "你好";
            ptreecolumn.Visible = true;
            ptreecolumn.VisibleIndex = 0;
            this.treeList1.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] { ptreecolumn });

最后在窗体加载事件内调用
[C#] 纯文本查看 复制代码
 collection = Stuff.GetStuff();
            TreeListBind(0);




评分

参与人数 2贡献 +3 赞扬 +2 收起 理由
maple + 1 感谢分享
羽叶 + 3 + 1 很给力

查看全部评分

回复

使用道具 举报

0

精华

5273

贡献

5976

赞扬

管理员

帖子
1172
软币
22442
在线时间
4448 小时
注册时间
2013-6-7

黄马甲

发表于 2014-8-14 10:14:22 | 显示全部楼层
支持代码教程!
回复

使用道具 举报

0

精华

0

贡献

553

赞扬

帖子
95
软币
1190
在线时间
74 小时
注册时间
2017-8-2
发表于 2024-2-21 17:23:03 | 显示全部楼层
Thanks for Sharing.
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-11-21 18:00

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表