树型递归

本人QQ-554433626 2019-05-14 04:17:13
头现在有点闷,求个代码块!
需求如下:
现有树型List<Tree> tree

public class Tree
{
public string id { get; set; }

public string text { get; set; }

public List<Tree> children { get; set; }
}


我目前有组想展示的树ID List<string> treeIds
treeIds[0]的值对应只展示 tree = tree.FirstOrDefault(t=>t.id.Equals(treeIds[0]))
treeIds[1]的值对应只展示 tree[0].children = tree[0].children.FirstOrDefault(t=>t.id.Equals(treeIds[1]))
treeIds[2]的值对应只展示 tree[0].children[0].children= tree[0].children[0].children.FirstOrDefault(t=>t.id.Equals(treeIds[2]))

不知道对没对,解释意思应该就是:treeIds的顺序一一对应树型每个等级只展示的节点

得出筛选后的List<Tree> tree
...全文
68 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
this.ViewState["ds"] = ds; 绑定数据源 AddTree("", (TreeNode)null); 开始递归 public void AddTree(string ParentNO, TreeNode pNode) { DataSet ds = (DataSet)this.ViewState["ds"]; DataView dvTree = new DataView(ds.Tables[0]); //过滤ParentNO,得到当前的所有子节点 dvTree.RowFilter = "ParentNo ='" + ParentNO + "'"; foreach (DataRowView Row in dvTree) { TreeNode Node1 = new TreeNode(); if (pNode == null) { //添加根节点 Node1.Text = Row["DeptName"].ToString(); TvwCatalog.Nodes.Add(Node1); Node1.Expanded = true; Node1.NavigateUrl = "ShopProductList.aspx?DeptNo=" + Row["DeptNo"].ToString(); AddTree(Row["DeptNo"].ToString(), Node1); //再次递归 } else { //̀添加当前节点的子节点 Node1.Text = Row["DeptName"].ToString(); pNode.ChildNodes.Add(Node1); Node1.Expanded = true; Node1.NavigateUrl = "ShopProductList.aspx?DeptNo=" + Row["DeptNo"].ToString(); AddTree(Row["DeptNo"].ToString(), Node1); //再次递归 } } } 这就是递归树了, 你改改 就可以用了

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧