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

 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:TreeView ID="TreeView1" runat="server">
</asp:TreeView>
</td> </tr>
</table>
</div>
</form>
</body>
</html>

4、tvdemo.aspx.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data; public partial class controlDemo_tvdemoaspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ if(!IsPostBack)
{
//练习treview 静态手动添加节点
//TreeDemo(); //练习treview 动态递归添加节点
string sql = "select id,text,pid from CITY ";
DataClass dc = new DataClass();
TreeNode treen = null;
Bind_Tv(dc.GetDataTable(sql), treen, null, "id", "pid", "text"); //练习DataView 过滤结果集(RowFilter)和遍历结果集(foreach(DataRowView row in dv) )
//test();
} } #region 练习DataView 过滤结果集(RowFilter)和遍历结果集(foreach(DataRowView row in dv) )
public void test() {
DataClass dc = new DataClass();
TreeView TreeView1 = new TreeView(); string sql = "select id,text,pid from CITY ";
DataTable dt = new DataTable();
dt = dc.GetDataTable(sql); DataView dv = new DataView(dt); int lin = dv.Count; string filter = " pid = '1'";
dv.RowFilter = filter;//利用DataView将数据进行筛选,选出相同 父id值 的数据 int lin2 = dv.Count;
//foreach (DataRowView row in dv)
//{
// row["Item_ID"].ToString().Trim();
// row["Item_Name"].ToString().Trim();
//}
}
#endregion 练习DataView 过滤结果集(RowFilter)和遍历结果集(foreach(DataRowView row in dv) ) #region treview 静态手动添加节点 public void TreeDemo()
{
TreeNode tn = new TreeNode();
//tn.Value = "0";
tn.Text = "NBA";
TreeView1.Nodes.Add(tn); TreeNode hn = new TreeNode();
hn.Text = "Hoston";
tn.ChildNodes.Add(hn); TreeNode hpn = new TreeNode();
hpn.Text = "YaoMing";
hn.ChildNodes.Add(hpn); TreeNode ln = new TreeNode();
ln.Text = "Laker";
tn.ChildNodes.Add(ln); }
# endregion treview 静态手动添加节点 #region treview 动态递归添加节点
/// <summary>
/// 绑定TreeView(利用TreeNode)
/// </summary>
/// <param name="p_Node">TreeNode(TreeView的一个节点)</param>
/// <param name="pid_val">父id的值</param>
/// <param name="id">数据库 id 字段名</param>
/// <param name="pid">数据库 父id 字段名</param>
/// <param name="text">数据库 文本 字段值</param>
public void Bind_Tv(DataTable dt, TreeNode p_Node, string pid_val, string id, string pid, string text)
{
DataView dv = new DataView(dt);//将DataTable存到DataView中,以便于筛选数据
TreeNode tn;//建立TreeView的节点(TreeNode),以便将取出的数据添加到节点中
//以下为三元运算符,如果父id为空,则为构建“父id字段 is null”的查询条件,否则构建“父id字段=父id字段值”的查询条件
string filter = string.IsNullOrEmpty(pid_val) ? pid + " is null" : string.Format(pid + "='{0}'", pid_val);
dv.RowFilter = filter;//利用DataView将数据进行筛选,选出相同 父id值 的数据
//int lin = dv.Count; foreach (DataRowView row in dv)
{
tn = new TreeNode();//建立一个新节点(学名叫:一个实例)
if (p_Node == null)//如果为根节点
{
tn.Value = row[id].ToString().Replace(" ", "");//节点的Value值,一般为数据库的id值
tn.Text = row[text].ToString().Replace(" ", "");//节点的Text,节点的文本显示
TreeView1.Nodes.Add(tn);//将该节点加入到TreeView中
Bind_Tv(dt, tn, tn.Value, id, pid, text);//递归(反复调用这个方法,直到把数据取完为止)
//tn.Value除了根节点以后就是有值了!!!
}
else//如果不是根节点
{
tn.Value = row[id].ToString().Replace(" ", "");//节点Value值
tn.Text = row[text].ToString().Replace(" ", "");//节点Text值
p_Node.ChildNodes.Add(tn);//该节点加入到上级节点中
Bind_Tv(dt, tn, tn.Value, id, pid, text);//递归
}
}
} #endregion treview 动态递归添加节点
}

5、GetDataTable 返回Datatable方法

    public DataTable GetDataTable(string sql)
{
this.OpenConnection();
this.cmd = new SqlCommand(sql, this.conn);
this.sdap = new SqlDataAdapter(this.cmd);
this.ds = new DataSet();
sdap.Fill(ds);
newdt = ds.Tables[]; return newdt;
}

net TreeView 递归的更多相关文章

  1. WinForm 进程、线程、TreeView递归加载、发送邮件--2016年12月13日

    进程:一个程序就是一个进程,但是也有一个程序需要多个进程来支持的情况 进程要使用的类是:Process它在命名空间:System.Diagnostics; 静态方法Start(); Process.S ...

  2. winform进程、线程、TreeView递归加载

    进程: 一般来说,一个程序就是一个进程,不过也有一个程序需要多个进程支持的情况. 进程所使用的类:Process 所需命名空间:System.Diagnostics; 可以通过进行来开启计算机上现有的 ...

  3. TreeView递归绑定无限分类数据

    TreeView递归绑定无限分类数据 实现一个动态绑定,无限级分类数据时,需要将数据绑定到TreeView控件,分类表的结构是这样的: 字段 类型 Id int ParentId int Name N ...

  4. treeview递归加载

    实体类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...

  5. TreeView递归取值

    string jingyuan = ""; string jinghui = ""; private void DiGui(TreeNode tn) { if ...

  6. TreeView递归绑定数据的两种方法

    #region 绑定TreeView /// <summary> /// 绑定TreeView(利用TreeNode) /// </summary> /// <param ...

  7. WinForm TreeView递归加载

    这个其实通俗一点讲就是的树状分支图 首先利用递归添加数据 数据放入 treeView1.Nodes.Add() 中 public Form3() { InitializeComponent(); Tr ...

  8. WPF TreeView递归遍历相关方法

    /// <summary> /// 递归改变组织树选中状态. /// </summary> /// <param name="org">< ...

  9. treeview递归

    1.数据库 table A( ID int pk, Value varchar, Fid int ) A: ID   Value    Fid 1   value1     0 2   value2  ...

随机推荐

  1. Ninject之旅之六:Ninject约定

    摘要 在小的应用系统中一个一个注册一些服务类型不怎么困难.但是,如果是一个实际的有上百个服务的应用程序呢?约定配置允许我们使用约定绑定一组服务,而不用一个一个分别绑定. 要使用约定配置,需要添加Nin ...

  2. 用ARCGIS配出一张DEM专题图

    专题图是指突出而尽可能完善.详尽地表达制图区内的一种或几种自然或社会经济要素的地图.专题图的制图领域宽广,凡具有空间属性的信息数据都可以用其来表示.由于DEM描述的是地面高程信息,它在测绘.水文.气象 ...

  3. nice-validator验证插件

    主要是作为form表单的验证,密码,确认密码的验证,远程验证的功能: 1:先导包:nice-validator 2:引入文件css,js 3: 使用 使用文档:http://www.niceue.co ...

  4. centos 7.2 网卡配置文件 及 linux bridge的静态配置

    在 centos 7.2 系统内, 网卡的配置文件在: /etc/sysconfig/network-scripts/ 下. 命名规则: ifcfg-xxxx.   xxx为设备名称. 通过分析 ne ...

  5. information_schema系列十二

    1: INNODB_SYS_VIRTUAL 表存储的是INNODB表的虚拟列的信息,当然这个还是比较简单的,我们直接通过SHOW CREATE TABLE 或者DESC TABLE就能看得到. Col ...

  6. TFS Build Definition And Auto Deploy

    一台build machine上一般只有一个build service[对应一个build controller]来serve一个team project collection,但又workaroun ...

  7. SRM 146 DIV1 600

    Problem Statement      Masterbrain is a two player board game in which one player decides on a secre ...

  8. 【英语魔法俱乐部——读书笔记】 1 初级句型-简单句(Simple Sentences)

    第一部分 1 初级句型-简单句(Simple Sentences):(1.1)基本句型&补语.(1.2)名词短语&冠词.(1.3)动词时态.(1.4)不定式短语.(1.5)动名词.(1 ...

  9. truncate和delete之间有什么区别

    TRUNCATE TABLE 在功能上与不带 WHERE 子句的 DELETE 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 DELETE 速度快,且使用的系统和事务日志资源 ...

  10. 使用Linux碎解二

    承接上文碎解一.本章讲述,基本配置. 一.网络配置相关. error:(执行yum 命令时出现)Cannot find a valid baseurl for repo:base/7/x86_64 解 ...