我的treeview动态添加节点方法
程序员文章站
2022-06-08 09:16:46
...
调用方法
this.TreeView.Nodes.Clear();
TreeNode MainNode = new TreeNode();
MainNode.Text = "总分类";
MainNode.Name = "000";
SetTree(listProfe.ToList(), MainNode.Name,MainNode);
this.TreeView.Nodes.Add(MainNode);
将节点name设定为实体的Guid或者Id唯一性质的,将Text设定为实体的Name
public void SetTree(List<TrainingProfe> list,string Flag,TreeNode inNode)
{
List<TrainingProfe> listSe = new List<TrainingProfe>();
listSe = list.Where(x => x.PGuid == Flag).ToList();///先判断是否有,然后再添加
if (listSe.Any())
{
foreach (TrainingProfe one in list)
{
TreeNode oneNode = new TreeNode();
oneNode.Text = one.Name;
oneNode.Name = one.Guid;
oneNode.Tag = one;
//oneNode.Nodes.Add(SetTree(listSe,one.Guid,oneNode));
SetTree(list, oneNode.Name, oneNode);
oneNode.Checked = one.IsUsed;
inNode.Nodes.Add(oneNode);
}
}
}
上一篇: 桃仁的介绍与用法