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


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

    结果如图所示:

  • 相关阅读:
    sdnu 1513 字符串翻转
    hdu-1559 最大子矩阵(二维树状数组模板题)
    hdu-1556 树状数组
    1049.饭盒
    1092.校门外的树
    1012.区间合并
    1054.数独
    1175.开心的金明 01背包
    空格(Space)的ASCII码值是:32
    js为lable和div赋值
  • 原文地址:https://www.cnblogs.com/jinghuimin/p/5076047.html
Copyright © 2020-2023  润新知