HMJING
**过程虽可贵,结果价更高**

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());
}
}

结果如图所示:

posted on 2015-12-25 15:18  HMJING  阅读(8632)  评论(0编辑  收藏  举报