//设定生成树的原始数据voidgetdatable(){tblDatas.Columns.Add("groupid",Type.GetType("System.String"));tblDatas.Columns.Add("groupname",Type.GetType("System.String"));tblDatas.Columns.Add("parentid",Type.GetType("System.String"));tblDatas.Rows.Add(newobject[]{"1","机关","0"});tblDatas.Rows.Add(newobject[]{"2","学院","0"});tblDatas.Rows.Add(newobject[]{"3","教学管理中心","1"});tblDatas.Rows.Add(newobject[]{"4","校园管理中心","1"});tblDatas.Rows.Add(newobject[]{"5","数据中心","3"});tblDatas.Rows.Add(newobject[]{"6","信息中心","3"});tblDatas.Rows.Add(newobject[]{"7","一卡通","4"});tblDatas.Rows.Add(newobject[]{"8","保卫处","4"});tblDatas.Rows.Add(newobject[]{"9","信工系","2"});tblDatas.Rows.Add(newobject[]{"10","艺术系","2"});dataGridView1.DataSource=tblDatas;}//递归生成树函数publicvoidAddTree(intParentID,TreeNodepNode){DataTabledt=newDataTable();dt=tblDatas;DataViewdvTree=newDataView(dt);//过滤ParentID,得到当前的所有子节点dvTree.RowFilter="parentid="+ParentID;foreach(DataRowViewRowindvTree){TreeNodeNode=newTreeNode();if(pNode==null){//添加根节点Node.Text=Row["groupname"].ToString();treeView1.Nodes.Add(Node);AddTree(Int32.Parse(Row["groupid"].ToString()),Node);//再次递归}else{//添加当前节点的子节点Node.Text=Row["groupname"].ToString();pNode.Nodes.Add(Node);AddTree(Int32.Parse(Row["groupid"].ToString()),Node);//再次递归
2025/4/5 4:39:40
38KB
递归树生成
1