net TreeView 递归
1、显示效果
2、数据insert脚本
insert into CITY(id,text,pid) values('1','城市',null)
insert into CITY(id,text,pid) values('2','北京市','1')
insert into CITY(id,text,pid) values('3','上海市','1')
insert into CITY(id,text,pid) values('4','天津市','1')
insert into CITY(id,text,pid) values('5','重庆市','1')
insert into CITY(id,text,pid) values('6','湖北省','1')
insert into CITY(id,text,pid) values('7','河北省','1')
insert into CITY(id,text,pid) values('8','山西省','1')
insert into CITY(id,text,pid) values('9','武汉市','6')
insert into CITY(id,text,pid) values('10','黄冈市','6')
insert into CITY(id,text,pid) values('11','石家庄','7')
insert into CITY(id,text,pid) values('12','唐山市','7')
insert into CITY(id,text,pid) values('13','太原市','8')
insert into CITY(id,text,pid) values('14','吉林省','1')
insert into CITY(id,text,pid) values('15','长春市','14')
insert into CITY(id,text,pid) values('16','南关区','15')
3、tvdemo.aspx
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head runat="server"> 3 <title></title> 4 </head> 5 <body> 6 <form id="form1" runat="server"> 7 <div> 8 <table> 9 <tr> 10 <td> 11 <asp:TreeView ID="TreeView1" runat="server"> 12 </asp:TreeView> 13 </td> 14 15 </tr> 16 </table> 17 </div> 18 </form> 19 </body> 20 </html>
4、tvdemo.aspx.cs
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Data; 8 9 public partial class controlDemo_tvdemoaspx : System.Web.UI.Page 10 { 11 protected void Page_Load(object sender, EventArgs e) 12 { 13 14 if(!IsPostBack) 15 { 16 //练习treview 静态手动添加节点 17 //TreeDemo(); 18 19 //练习treview 动态递归添加节点 20 string sql = "select id,text,pid from CITY "; 21 DataClass dc = new DataClass(); 22 TreeNode treen = null; 23 Bind_Tv(dc.GetDataTable(sql), treen, null, "id", "pid", "text"); 24 25 //练习DataView 过滤结果集(RowFilter)和遍历结果集(foreach(DataRowView row in dv) ) 26 //test(); 27 } 28 29 } 30 31 #region 练习DataView 过滤结果集(RowFilter)和遍历结果集(foreach(DataRowView row in dv) ) 32 public void test() { 33 DataClass dc = new DataClass(); 34 TreeView TreeView1 = new TreeView(); 35 36 string sql = "select id,text,pid from CITY "; 37 DataTable dt = new DataTable(); 38 dt = dc.GetDataTable(sql); 39 40 DataView dv = new DataView(dt); 41 42 int lin = dv.Count; 43 44 string filter = " pid = '1'"; 45 dv.RowFilter = filter;//利用DataView将数据进行筛选,选出相同 父id值 的数据 46 47 48 int lin2 = dv.Count; 49 //foreach (DataRowView row in dv) 50 //{ 51 // row["Item_ID"].ToString().Trim(); 52 // row["Item_Name"].ToString().Trim(); 53 //} 54 } 55 #endregion 练习DataView 过滤结果集(RowFilter)和遍历结果集(foreach(DataRowView row in dv) ) 56 57 #region treview 静态手动添加节点 58 59 public void TreeDemo() 60 { 61 TreeNode tn = new TreeNode(); 62 //tn.Value = "0"; 63 tn.Text = "NBA"; 64 TreeView1.Nodes.Add(tn); 65 66 TreeNode hn = new TreeNode(); 67 hn.Text = "Hoston"; 68 tn.ChildNodes.Add(hn); 69 70 TreeNode hpn = new TreeNode(); 71 hpn.Text = "YaoMing"; 72 hn.ChildNodes.Add(hpn); 73 74 75 TreeNode ln = new TreeNode(); 76 ln.Text = "Laker"; 77 tn.ChildNodes.Add(ln); 78 79 } 80 # endregion treview 静态手动添加节点 81 82 83 #region treview 动态递归添加节点 84 /// <summary> 85 /// 绑定TreeView(利用TreeNode) 86 /// </summary> 87 /// <param name="p_Node">TreeNode(TreeView的一个节点)</param> 88 /// <param name="pid_val">父id的值</param> 89 /// <param name="id">数据库 id 字段名</param> 90 /// <param name="pid">数据库 父id 字段名</param> 91 /// <param name="text">数据库 文本 字段值</param> 92 public void Bind_Tv(DataTable dt, TreeNode p_Node, string pid_val, string id, string pid, string text) 93 { 94 DataView dv = new DataView(dt);//将DataTable存到DataView中,以便于筛选数据 95 TreeNode tn;//建立TreeView的节点(TreeNode),以便将取出的数据添加到节点中 96 //以下为三元运算符,如果父id为空,则为构建“父id字段 is null”的查询条件,否则构建“父id字段=父id字段值”的查询条件 97 string filter = string.IsNullOrEmpty(pid_val) ? pid + " is null" : string.Format(pid + "='{0}'", pid_val); 98 dv.RowFilter = filter;//利用DataView将数据进行筛选,选出相同 父id值 的数据 99 //int lin = dv.Count; 100 101 foreach (DataRowView row in dv) 102 { 103 tn = new TreeNode();//建立一个新节点(学名叫:一个实例) 104 if (p_Node == null)//如果为根节点 105 { 106 tn.Value = row[id].ToString().Replace(" ", "");//节点的Value值,一般为数据库的id值 107 tn.Text = row[text].ToString().Replace(" ", "");//节点的Text,节点的文本显示 108 TreeView1.Nodes.Add(tn);//将该节点加入到TreeView中 109 Bind_Tv(dt, tn, tn.Value, id, pid, text);//递归(反复调用这个方法,直到把数据取完为止) 110 //tn.Value除了根节点以后就是有值了!!! 111 } 112 else//如果不是根节点 113 { 114 tn.Value = row[id].ToString().Replace(" ", "");//节点Value值 115 tn.Text = row[text].ToString().Replace(" ", "");//节点Text值 116 p_Node.ChildNodes.Add(tn);//该节点加入到上级节点中 117 Bind_Tv(dt, tn, tn.Value, id, pid, text);//递归 118 } 119 } 120 } 121 122 #endregion treview 动态递归添加节点 123 }
5、GetDataTable 返回Datatable方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public DataTable GetDataTable(string sql) 2 { 3 this.OpenConnection(); 4 this.cmd = new SqlCommand(sql, this.conn); 5 this.sdap = new SqlDataAdapter(this.cmd); 6 this.ds = new DataSet(); 7 sdap.Fill(ds); 8 newdt = ds.Tables[0]; 9 10 return newdt; 11 }