文件名称:C#递归树的生成级Treeview空间的应用实例源码
文件大小:38KB
文件格式:ZIP
更新时间:2018-08-04 02:43:47
递归树生成
//设定生成树的原始数据 void getdatable() { 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(new object[] { "1", "机关", "0" }); tblDatas.Rows.Add(new object[] { "2", "学院", "0" }); tblDatas.Rows.Add(new object[] { "3", "教学管理中心", "1" }); tblDatas.Rows.Add(new object[] { "4", "校园管理中心", "1" }); tblDatas.Rows.Add(new object[] { "5", "数据中心", "3" }); tblDatas.Rows.Add(new object[] { "6", "信息中心", "3" }); tblDatas.Rows.Add(new object[] { "7", "一卡通", "4" }); tblDatas.Rows.Add(new object[] { "8", "保卫处", "4" }); tblDatas.Rows.Add(new object[] { "9", "信工系", "2" }); tblDatas.Rows.Add(new object[] { "10", "艺术系", "2" }); dataGridView1.DataSource = tblDatas; } //递归生成树函数 public void AddTree(int ParentID, TreeNode pNode) { DataTable dt = new DataTable(); dt = tblDatas; DataView dvTree = new DataView(dt); //过滤ParentID,得到当前的所有子节点 dvTree.RowFilter = "parentid = " + ParentID; foreach (DataRowView Row in dvTree) { TreeNode Node = new TreeNode(); 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); //再次递归 } } } //调用递归函数在treeView1里面显示给出数据的树形图 private void button1_Click(object sender, EventArgs e) { treeView1.Nodes.Clear(); AddTree(0, (TreeNode)null); }
【文件预览】:
WindowsApplication1
----WindowsApplication2()
--------Form1.Designer.cs(4KB)
--------Program.cs(478B)
--------obj()
--------bin()
--------Form1.cs(4KB)
--------Form1.resx(6KB)
--------WindowsApplication2.csproj(3KB)
--------Properties()
----WindowsApplication1.sln(946B)
----WindowsApplication1.suo(17KB)