- 积分
- 62
- 在线时间
- 101 小时
- 主题
- 16
- 注册时间
- 2013-8-21
- 帖子
- 104
- 最后登录
- 2024-11-7
- 帖子
- 104
- 软币
- 876
- 在线时间
- 101 小时
- 注册时间
- 2013-8-21
|
本帖最后由 贾林朋 于 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);
|
评分
-
查看全部评分
|