惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MongoDB | Blog
MongoDB | Blog
Recorded Future
Recorded Future
Jina AI
Jina AI
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
人人都是产品经理
人人都是产品经理
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
L
LangChain Blog
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
博客园 - 【当耐特】
雷峰网
雷峰网
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
有赞技术团队
有赞技术团队
L
Lohrmann on Cybersecurity
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
P
Privacy & Cybersecurity Law Blog
Scott Helme
Scott Helme
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Hacker News - Newest:
Hacker News - Newest: "LLM"
NISL@THU
NISL@THU
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog RSS Feed
Cyberwarzone
Cyberwarzone
K
Kaspersky official blog
F
Full Disclosure
Martin Fowler
Martin Fowler
Spread Privacy
Spread Privacy
D
Docker
C
Cisco Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page

博客园 - 寸芒

[原创]作业:家庭消费管理程序,一点代码 [原创]JScript学习摘要1 [随]程序设计模式的有趣解释-追MM [随]SQL Server 和ACCESS 、EXCEL 的数据转换 [随]测试了下我的Blog价值(汗颜了) [随]C#轻松解决世纪迷题 [随]下了个windows live write 来玩玩 [随] 网站注册随机码的实现 [随]存储过程入门与提高 [原创]涂鸦事件 [随]c#.net常用函数和方法 [原创]到底调用哪个方法 [原创]浅要分析委托和事件 [原创]readonly 和const分析 [随]抽象基类与接口,共性与个性的选择 [原创]Box and Unbox [随]c#连接数据库 [随]八荣八耻 [原创]引用类型的转换之感想
[原创]treeview简单根据数据库的数据,显示多级节点
寸芒 · 2007-08-21 · via 博客园 - 寸芒

如图:

1

要建立多少个数据表???其实一个数据库,一张表就足够,自我调用的形式:

--创建个数据库
create database live
use live
--创建个表
create table live
(
 cid int identity(1,1) primary key not null,
 class char(20) not null,
 pcid int not null
)
select * from live
--插入根节点的标题
insert into live values('吃',0)
insert into live values('穿',0)
insert into live values('住',0)
insert into live values('行',0)
--插入子节点的标题
insert into live values('西餐',1)
insert into live values('中餐',1)
insert into live values('嘻哈系列',2)
insert into live values('正装系列',2)
insert into live values('牛仔系列',2)
insert into live values('瓦片房',3)
insert into live values('茅草房',3)
insert into live values('白坯房',3)
insert into live values('精装小屋',3)
insert into live values('两个轮子的',4)
insert into live values('四个轮子的',4)
--插入子结点的子节点的标题
insert into live values('奔驰',15)
insert into live values('奥迪',15)
insert into live values('尼桑',15)
insert into live values('别克',15)

--------------------

以上的是创建数据库,并插入数据,具体实现代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace treeviewANDdatabase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
            //连接数据库
            SqlConnection conn = new SqlConnection("server=admin;uid=sa;pwd=sa;database=live");
            conn.Open();
            //数据的筛选
            string str = "select * from live";
            //DataAdapter,它起着桥梁的作用,在 DataSet 和其源数据存储区之间进行数据检索和保存
            SqlDataAdapter da = new SqlDataAdapter(str, conn);
            //填充 dataset 具有类似数据库的结构,如表、列、关系和约束!
            DataSet ds = new DataSet();
            da.Fill(ds);
            //调用方法
            nodes(this.treeView1.Nodes, ds, 0);
           
        }
        //方法
        private void nodes(TreeNodeCollection tnode,DataSet ds, int id)
        {
            //创建视图 ,应为只有一张表
            DataView dview = new DataView(ds.Tables[0]);
            //视图的好处,在于能方便筛选数据
            dview.RowFilter = "[pcid]=" + id+ "";
            //显示每个节点的标题,递归!
            foreach (DataRowView row in dview)
            {
                TreeNode  node = new TreeNode();
                node.Text = row["class"].ToString();
                tnode.Add(node);
                nodes(node.Nodes, ds,Int32.Parse( row["cid"].ToString()));
            }
        }
       
    }
}

---------------------------------------------

OK,就这么简单。。。当然,treeview的应用远远没那么简单!