码迷,mamicode.com
首页 > Windows程序 > 详细

C# TreeView的用法(递归加载节点内容)

时间:2015-12-25 16:44:37      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Orag> list = new List<Orag>();
private void Form1_Load(object sender, EventArgs e)
{
list = new List<Orag> { new Orag(1, 0, "上海公司"), new Orag(5, 0, "北京公司"), new Orag(2, 1, "上海公司1"), new Orag(3, 1, "上海公司2"), new Orag(7, 3, "上海公司2-1"), new Orag(4, 1, "上海公司3"), new Orag(6, 5, "北京公司1") };
Addnode();
}

public void Addnode()
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].pid==0)
{
TreeNode pnode = new TreeNode();
pnode.Text = list[i].name;
pnode.Tag = list[i].id;
treeView1.Nodes.Add(pnode);
AddChildnode(list[i].id,pnode);
}
}
}

public void AddChildnode(int pid,TreeNode pnode)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].pid == pid)
{
TreeNode cnode = new TreeNode();
cnode.Text = list[i].name;
cnode.Tag = list[i].id;
pnode.Nodes.Add(cnode);
AddChildnode(list[i].id,cnode);
}
}
}

public class Orag
{
public int id { get; set; }
public int pid { get; set; }
public string name { get; set; }

public Orag(int id,int pid,string name)
{
this.id = id;
this.pid = pid;
this.name = name;
}
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
MessageBox.Show(treeView1.SelectedNode.Tag.ToString());
}
}

结果如图所示:

技术分享

C# TreeView的用法(递归加载节点内容)

标签:

原文地址:http://www.cnblogs.com/jinghuimin/p/5076047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!