C#递归所以部门展示到TreeView

1.首先是数据库表的设计

  新建一张部门表:TestUser表

  1.ID自增int主键 2.DeptName:nchar(10)3.DeptCode:nchar(10)4:ParentID:nchar(10)

2.部门表就建好了

3.新建一个网站

4.在网站的default.aspx界面拖一个TreeView控件。

5.aspx.cs代码如下:

public static string strConn=ConfigurationManager.ConnectionStrings["connString"].ConnectionString.ToString();//数据库连接串

protected void Page_Load(object sender,EventArgs e)
{
if(!isPostBack)
{
BindDeptTree("0");//数据库你也可以设计成int类型
}
} private void BindDeptNode(TreeNode DTnode)
{
try
{
DataSet=reDs("select DeptName,DeptCode from TestUser where ParentID='"+DTnode.Value+"'");
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
TreeNode node=new TreeNode();
node.Text=ds.Tables[0].Rows[i]["DeptName"].ToString();
node.Value=ds.Tables[0].Rows[i][DeptCode].ToString();
DTnode.ChildNodes.Add(node);//把指定的节点添加到控件中
BindDeptNode(node);
}
}
catch(Exception ex)
{
Log.LogWrite(ex.Message);
}
} private void BindDeptTree(string ParentID)
{
DataSet=reDs("select DeptName,DeptCode from TestUser where ParentID='"+DTnode.Value+"'");
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
TreeNode node=new TreeNode();
node.Text=ds.Tables[0].Rows[i]["DeptName"].ToString();
node.Value=ds.Tables[0].Rows[i][DeptCode].ToString();
tvData.Nodes.Add(node);//TreeView的IDtvData
BindDeptNode(node);
}
} public DataSet reDs(string strSql)
{
using(SqlConnection conn=new Sqlconnection(strConn))
{
conn.Open();
DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter (strSql,conn);
da.Fill(ds);
conn.close();
return ds;
}
}

运行结果如下图:

希望对大家有帮助。有不足的地方望指教。尊重原创,转载请注明出处。

C#递归所以部门展示到TreeView的更多相关文章

  1. WPF下递归生成树形数据绑定到TreeView上

    最终效果图:(用于学习类的效果 图片丑了点,看官莫怪) 新建窗体 然后在前端适当位置插入如下代码: <TreeView x:Name="> <TreeView.ItemTe ...

  2. ASP.NET 递归将分类绑定到 TreeView

    CREATE TABLE [dbo].[sysMenuTree]([NoteId] [decimal](18, 0) NOT NULL,[ParentId] [decimal](18, 0) NULL ...

  3. 部门子部门表结构,递归指定部门的所有子部门SQL函数

  4. Java递归获取部门树 返回jstree数据

    @GetMapping("/getDept")@ResponseBodypublic Tree<DeptDO> getDept(String deptId){ Tree ...

  5. 潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据

    原文:潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据 目前自己对treeview的感慨很多 今天先讲 面对这种 表结构的数据 的其中 ...

  6. net TreeView 递归

     1.显示效果 2.数据insert脚本 insert into CITY(id,text,pid) values('1','城市',null)insert into CITY(id,text,pid ...

  7. SQL笔记 - CTE递归实例:显示部门全称

    昨天在整理JS的Function时,示例是一个递归函数.说起递归,想起前段时间在搞CTE,那个纠结呀,看似容易,可我总抓不住门道,什么递归条件,什么结束条件,一头雾水...今天一大早就爬起来,果然不负 ...

  8. bootstrap treeview 树形数据生成

    这个问题还是挺经典的,后台只是负责查出所有的数据,前台js来处理数据展示给treeview;show you the code below:<script> $(function () { ...

  9. TreeView 树节点的处理

    TreeView 树节点的处理 using System; using System.Collections.Generic; using System.ComponentModel; using S ...

随机推荐

  1. mysql查询结果带上序号

    select (@i:=@i+1) as rownum,t1.id ","from mega_user t1,(select @i:=0) t2 order by t1.gold ...

  2. Makefile的补充学习2

    Makefile中使用通配符(1)* 若干个任意字符(2)? 1个任意字符(3)[] 将[]中的字符依次去和外面的结合匹配 还有个%,也是通配符,表示任意多个字符,和*很相似,但是%一般只用于规则描述 ...

  3. WPF控件开源资源

    (转)WPF控件开源资源 Textbox Drag/Drop in WPFhttp://www.codeproject.com/Articles/42696/Textbox-Drag-Drop-in- ...

  4. notepad++去空格空行技巧

    选择视图显示所有字符,替换成空的就行

  5. L143 Seasonal 'Plague' Hits College Freshman

    Sometimes, all the hand sanitizer in the world cannot prevent the inevitable.College freshmen across ...

  6. New Concept English three (31)

    35w/m 45 True eccentrics never deliberately set out to draw attention to themselves. They disregard ...

  7. SlowHTTPTest-慢速DoS攻击

    Slowhttptest是一个依赖于实际HTTP协议的Slow HTTP DoS攻击工具,它的设计原理是要求服务器所有请求被完全接收后再进行处理. SlowHTTPTest是一款对服务器进行慢攻击的测 ...

  8. linux(redhat) mysql 的安装

    教程链接 注意 1.检查版本32/64位的时候 输入 name -a 输出为 Linux localhost.localdomain 2.6.18-53.el5 #1 SMP Wed Oct 10 1 ...

  9. 【转载】取得系统中网卡MAC地址的三种方法

    From:http://blog.csdn.net/zhangting1987/article/details/2732135 网卡地址这个概念有点混淆不清.因为实际上有两个地址,mac地址和物理地址 ...

  10. Spring IOC容器的初始化-(三)BeanDefinition的注册

    ---恢复内容开始--- 前言 在上一篇中有一处代码是BeanDefiniton注册的入口,我们回顾一下. 1.BeanDefiniton在IOC容器注册 首先我们回顾两点,1. 发起注册的地方:2. ...