在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
效果图如下:
create table treedata (
id int IDENTITY(1,1) NOT NULL , context varchar(50) NOT NULL , parentid int NOT NULL );
id:当前的id,context当前节点的值文本,parentid代表当前节点的父节点id
insert into treedata values(1, '所有部门', 0);
insert into treedata values(2, '人事部', 1); insert into treedata values(3, '财务部', 1); insert into treedata values(4, '生产部', 1); insert into treedata values(5, '车间一', 4); insert into treedata values(6, '车间二', 4); insert into treedata values(7, '车间三', 4); insert into treedata values(8, '商务部', 1); 再用递归根据数据库查询的值动态绑定treeView <asp:TreeView ID="treeView1" runat="server"></asp:TreeView>
public class SqlScript
{ public SqlScript() { // //TODO: 在此处添加构造函数逻辑 // } public string selParentID = " select id, context, parentid from treedata "; }
using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using BLL; using System.Data; public partial class test2 : System.Web.UI.Page { SqlScript sql = new SqlScript(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { LoadTree(0, (TreeNode)null); treeView1.ExpandAll();//默认展开所有节点 //this.toolStripComboBox1.SelectedIndex = 0; } } public void LoadTree(int ParentID, TreeNode pNode) { DataTable dt = SQLHandler.SearchData(sql.selParentID); TreeNode tn1 = new TreeNode(); DataView dvTree = new DataView(dt); dvTree.RowFilter = "[PARENTID] = " + ParentID;//过滤ParentID,得到当前的所有子节点 foreach (DataRowView Row in dvTree) { if (pNode == null) { //'?添加根节点 tn1.Text = Row["ConText"].ToString(); treeView1.Nodes.Add(tn1); tn1.ExpandAll(); LoadTree(Int32.Parse(Row["ID"].ToString()), tn1); //再次递归 } else { //添加当前节点的子节点 TreeNode tn2 = new TreeNode(); tn2.Text = Row["ConText"].ToString(); pNode.ChildNodes.Add(tn2); tn1.ExpandAll(); LoadTree(Int32.Parse(Row["ID"].ToString()), tn2); //再次递归 } } treeView1.ExpandAll(); } }
http://blog.csdn.net/qq634416025/article/details/7624801 |
请发表评论