Use of the C # TreeView (Recursive Loading of node content)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.