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, "Shanghai Company"), New Orag (5, 0, "Beijing Company"), New Orag (2, 1, "Shanghai Company 1"), New Orag (3, 1, "Shanghai public Division 2 "), New Orag (7, 3," Shanghai company 2-1 "), New Orag (4, 1," Shanghai Company 3 "), New Orag (6, 5," Beijing Company 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 ());
}
}
Results:
Use of the C # TreeView (Recursive Loading of node content)