一 数据库创建:
create table DiGui(id int identity,parentid int //父级id ,name nvarchar(20))insert into DiGui values(0,\'河北\')insert into DiGui values(0,\'河南\')insert into DiGui values(1,\'邯郸\')insert into DiGui values(1,\'石家庄\')insert into DiGui values(2,\'郑州\')insert into DiGui values(2,\'安阳\')insert into DiGui values(3,\'峰峰\')insert into DiGui values(3,\'邯山区\')
二 递归实例:
//dt 的sql= select id,parentid,name from diguipublic void f(TreeNode tn, int id){DataRow[] rows = dt().Select(\"parentid=\" + id);foreach (var row in rows){TreeNode tnode = new TreeNode();tnode.Text = row[\"name\"].ToString();tnode.Value = row[\"id\"].ToString();int parentid = Convert.ToInt32(row[\"id\"]);if (id == 0){//一级节点TreeView1.Nodes.Add(tnode);f(tnode, parentid);}else{//递归出二三级节点tn.ChildNodes.Add(tnode);f(tnode, parentid);}}}
三 页面实现效果: