http://bbs.csdn.net/topics/370139193

SQL code

 

?

1
2
3
4
5
6
CREATE TABLE [dbo].[Company] (
    [Id] [int] IDENTITY (1, 1)  Primary Key NOT NULL ,
    [Levers] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [LevelId] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [Name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL 
)

C# code

 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
public partial class Form1 : Form
    {
        public string rootId = "-1";
 
        public Form1()
        {
            InitializeComponent();
            AddRootCompany(rootId);
            AddSubCompany();
        }
 
        /// <summary>
        /// 添加总公司
        /// </summary>
        /// <param name="rootId"></param>
        private void AddRootCompany(string rootId)
        {
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection("Data Source=localhost;uid=sa;pwd=saiyang;Database=CSDN"))
            {
                con.Open();
                string strSQL = "select * from Company where Levers='" + rootId + "'";
                using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con))
                {
                    adapter.Fill(ds);
                }
                TreeNode NewNode = new TreeNode();
                NewNode.Text = ds.Tables[0].Rows[0]["Name"].ToString().Trim();
                this.treeView1.Nodes.Add(NewNode);
            }
        }
 
        /// <summary>
        /// 添加子公司
        /// </summary>
        private void AddSubCompany()
        {
            DataSet ds = getMenuByLevel(rootId.ToString());
            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
            {
                InitTreeCompanyChildNode(treeView1.Nodes[j], ds.Tables[0].Rows[j]["LevelId"].ToString());
            }
 
        }
 
        /// <summary>
        /// 获取层次级别
        /// </summary>
        /// <param name="getparams"></param>
        /// <returns></returns>
        public DataSet getMenuByLevel(string param)
        {
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=CSDN;User ID=sa;Password=saiyang"))
            {
                con.Open();
                string strSQL = "select * from Company where Levers='" + param + "'";
                using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con))
                {
                    adapter.Fill(ds);
                }
            }
            return ds;
        }
 
        /// <summary>
        /// 递归获取子节点
        /// </summary>
        /// <param name="PNode"></param>
        /// <param name="classParentID"></param>
        private void InitTreeCompanyChildNode(TreeNode pNode, string classParentID)
        {
            DataSet ds = getMenuByLevel(classParentID);
            DataView dataView = new DataView();
            dataView = ds.Tables[0].DefaultView;
            foreach (DataRowView drv in dataView)
            {
                string id = drv["LevelId"].ToString();
                string name = drv["Name"].ToString();
                TreeNode NewNode = new TreeNode();
                //将子公司添加到父节点下面
                NewNode.Text = name;
                pNode.Nodes.Add(NewNode);
                InitTreeCompanyChildNode(NewNode, id);
            }
        }
 
    }

Winform中的Treeview动态绑定数据库的更多相关文章

  1. winform中生成TreeView树

    无论是webform还是winform,TreeView都是常用功能.使用递归方法很方便. 下面分享一个小实例. 数据库中3个字段,分别是:ID,itemType_name,itemType_PID ...

  2. winform中的TreeView的数据绑定

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

  3. C# 中的treeview绑定数据库(递归算法)

    近日面试的给我两道题目,一道是IQ测试,第二个就是题目所言 总共两个表 department(id int not null primary key,parentid int,name char(50 ...

  4. WinForm开发中针对TreeView控件改变当前选择节点的字体与颜色

    本文转载:http://www.cnblogs.com/umplatform/archive/2012/08/29/2660240.html 在B/S开发中,对TreeView控件要改变当前选中节点的 ...

  5. WinForm控件TreeView 只部分节点显示 CheckBox

    WinForm控件TreeView 只部分节点显示  CheckBox 用过asp.net的应该知道,要在treeview中实现上述功能可以使用ShowCheckBox 属性指定那些节点显示check ...

  6. WinForm中从SQLite数据库获取数据显示到DataGridView

    1.关于Sqlite Sqlite是一款开源的.适合在客户端和嵌入式设备中使用的轻量级数据库,支持标准的SQL. 不像SqlServer或Oracle的引擎是一个独立的进程.通过TCP或命名管道等与程 ...

  7. Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼

    Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼标签: winform treeview drawnode Treeview控 ...

  8. DropDownList怎样动态绑定数据库中的某一条数据

    用Ajax动态绑定数据库的数据:点击后台查看代码,编写代码如下 if (!IsPostBack) { using (SnailTechDataContext con = new SnailTechDa ...

  9. winform中DataGridView实现分页功能

    WinForm轻松实现自定义分页 (转载) WinForm轻松实现自定义分页 (转载)   转载至http://xuzhihong1987.blog.163.com/blog/static/26731 ...

随机推荐

  1. K-means算法实现

    目录 K-means K-means x = xlsread("D:\MatlabData\西瓜数据集.xlsx"); m = length(x); [Idx,C]=kmeans( ...

  2. chrome json 格式化插件 JSON-Handle

    Chrome 浏览器插件安装方法: 在地址栏输入  , 将下载的 .crx 插件包拖放到打开的页面中. JSON-Handle It's a browser and editor for JSON d ...

  3. 爬虫2.5-scrapy框架-下载中间件

    目录 scrapy框架-下载中间件 scrapy框架-下载中间件 middlewares.py中有两个类,一个是xxSpiderMiddleware类 一个是xxDownloaderMiddlewar ...

  4. PLSQL变量和类型,流程控制语句,集合

    ---PLSQL 调试授权 GRANT debug any procedure, debug connect session TO scott; --定义变量 declare part_number ...

  5. JS中自定义事件的使用与触发

    1. 事件的创建 JS中,最简单的创建事件方法,是使用Event构造器: var myEvent = new Event('event_name'); 但是为了能够传递数据,就需要使用 CustomE ...

  6. RIGHT-BICEP测试第二次

    1.Right-结果是否正确? 正确 2.B-是否所有的边界条件都是正确的? 正确 3.P-是否满足性能要求? 部分满足 4.是否满足有无括号? 无 5.数字个数是否不超过十? 只是双目运算 6.能否 ...

  7. tensorflow之分类学习

    写在前面的话 MNIST教程是tensorflow中文社区的第一课,例程即训练一个 手写数字识别 模型:http://www.tensorfly.cn/tfdoc/tutorials/mnist_be ...

  8. 利用p4实现ipv6转发实验

    写在前面 只是作为一个入门p4的实验尝试,借用了一些即成的运行代码. p4代码 /**p4_16,v1_model**/ #include<core.p4> #include<v1m ...

  9. lintcode-206-区间求和 I

    206-区间求和 I 给定一个整数数组(下标由 0 到 n-1,其中 n 表示数组的规模),以及一个查询列表.每一个查询列表有两个整数 [start, end] . 对于每个查询,计算出数组中从下标 ...

  10. 初识ES6 解构

    1.数组的解构 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构 例子: let [a, b, c] = [1, 2, 3]; console.log(a);//1cons ...