using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
List<NodeInfo> nodeInfoList = new List<NodeInfo>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Init();
LoadData(new NodeInfo(Guid.Empty, "", Guid.Empty),null);
}
public void Init()
{
NodeInfo info1 = new NodeInfo(Guid.NewGuid(),"Node1",Guid.Empty);
NodeInfo info11 = new NodeInfo(Guid.NewGuid(), "Node11", info1.ID);
NodeInfo info111 = new NodeInfo(Guid.NewGuid(), "Node111", info11.ID);
NodeInfo info2 = new NodeInfo(Guid.NewGuid(), "Node2", Guid.Empty);
NodeInfo info21 = new NodeInfo(Guid.NewGuid(), "Node21", info2.ID);
NodeInfo info22 = new NodeInfo(Guid.NewGuid(), "Node22", info2.ID);
NodeInfo info3 = new NodeInfo(Guid.NewGuid(), "Node3", Guid.Empty);
NodeInfo info31 = new NodeInfo(Guid.NewGuid(), "Node31", info3.ID);
NodeInfo info311 = new NodeInfo(Guid.NewGuid(), "Node311", info31.ID);
nodeInfoList.Add(info1);
nodeInfoList.Add(info11);
nodeInfoList.Add(info111);
nodeInfoList.Add(info2);
nodeInfoList.Add(info21);
nodeInfoList.Add(info22);
nodeInfoList.Add(info3);
nodeInfoList.Add(info31);
nodeInfoList.Add(info311);
}
public void LoadData(NodeInfo nodeInfo,TreeNode parentNode)
{
List<NodeInfo> list =new List<NodeInfo>();
list = GetChildrenList(nodeInfo);
if (list == null || list.Count == )
{
return;
}
foreach (NodeInfo info in list)
{
if (info.ParentID == nodeInfo.ID)
{
TreeNode node = new TreeNode(info.Name);
node.Tag = info;
if (info.ParentID == Guid.Empty)
{
treeView1.Nodes.Add(node);
}
else
{
parentNode.Nodes.Add(node);
}
LoadData(info, node);
}
}
}
private List<NodeInfo> GetChildrenList(NodeInfo nodeInfo)
{
List<NodeInfo> list = new List<NodeInfo>();
list = nodeInfoList.FindAll(o => o.ParentID == nodeInfo.ID);
return list;
}
}
public class NodeInfo
{
public Guid ID { get; set; }
public string Name { get; set; }
public Guid ParentID { get; set; }
public NodeInfo(Guid id, string name, Guid parentId)
{
this.ID = id;
this.Name = name;
this.ParentID = parentId;
}
}
}