TreeView 绑定SQL

代码复制粘贴就能运行。

create database CityDB
go
use CityDB
go
create table CityTB
(
    cid int primary key not null,--编号
    cname varchar(10) not null,--名称
    cparentID int not null --父节点,对应编号
);
go
select * from CityTB

insert into CityTB values(100,'中国',0)
insert into CityTB values(201,'广西',100)
insert into CityTB values(202,'广东',100)
insert into CityTB values(301,'南宁',201)
insert into CityTB values(302,'柳州',201)
insert into CityTB values(303,'桂林',201)
insert into CityTB values(304,'广州',202)
insert into CityTB values(305,'深圳',202)

C#winfom
-------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
//树
namespace Tree2
{  
    //窗体
    public partial class Form1 : Form
    {
        public Form1()
        {      
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.SuspendLayout();
            // treeView1
            this.treeView1.Location = new System.Drawing.Point(12, 12);
            this.treeView1.Name = "treeView1";
            this.treeView1.Size = new System.Drawing.Size(154, 137);
            this.treeView1.TabIndex = 0;
            this.treeView1.NodeMouseDoubleClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseDoubleClick);
            // Form1
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(175, 165);
            this.Controls.Add(this.treeView1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
        }      
        //连接数据库
        public SqlConnection con = null;
        public SqlConnection ConOpen()
        {
            string sqlconstr = "server=127.0.0.1;database=CityDB;uid=sa;pwd=123";
            if (con == null)
            {
                con = new SqlConnection(sqlconstr);
                con.Open();
            }
            else if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            else if (con.State == ConnectionState.Broken)
            {
                con.Close();
                con.Open();
            }
            return con;
       }
        //关闭连接
        public void ConClose()
        {
            if (con != null)
            {
                con.Close();
            }
        }
        //操作数据库
        public SqlDataReader RunRead(string _sqlstr)
        {
            this.ConOpen();
            SqlCommand cmd = new SqlCommand(_sqlstr, con);
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return dr;
        }
        public DataSet RunDview(string _sqlstr, params SqlParameter[] value)
        {
            this.ConOpen();
            SqlDataAdapter adp = new SqlDataAdapter(_sqlstr, con);
            adp.SelectCommand.CommandType = CommandType.Text;
            adp.SelectCommand.Parameters.AddRange(value);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            this.ConClose();
            return ds;
        }
        public string Sqlstr(int _i)
        {
            string sql_condiction = "";
            switch (_i)
            {
                case 1: sql_condiction = "0";
                    break;
                case 2: sql_condiction = "@nodeID";
                    break;
            }
            string sql = "select cid,cname from CityTB where cparentID=" + sql_condiction;
            return sql;
        }
        //父节点
        public List<Treev> Man()
        {
            List<Treev> listTree = new List<Treev>();
            SqlDataReader read = this.RunRead(Sqlstr(1));
            while (read.Read())
            {
                Treev tree = new Treev();
                tree.Cid = (int)read["Cid"];
                tree.Cname = read["Cname"].ToString();
                listTree.Add(tree);
            }
            this.ConClose();
            return listTree;
        }
        public void ManBind()
        {
            List<Treev> listTree = Man();
            DataView dv = new DataView();
            foreach (Treev tree in listTree)
            {
                TreeNode node = new TreeNode();
                node.Tag = tree.Cid;//这样好好获取ID
                node.Text = tree.Cname;
                node.Expand();
                treeView1.Nodes.Add(node);
                SonBind(node,(int)node.Tag);
            }
        }
        public void SonBind(TreeNode _mannode, int _nodeID)
        {
            DataSet ds = Manson(_nodeID);
            DataView dv = ds.Tables[0].DefaultView;
            foreach (DataRowView row in dv)
            {
                TreeNode node = new TreeNode();
                node.Tag = row["Cid"];
                node.Text = row["Cname"].ToString();
                node.Expand();
                //添加子节点
                _mannode.Nodes.Add(node);
                SonBind(node, (int)node.Tag);
            }
        }
        //子节点
        public DataSet Manson(int _nodeID)
        {
            SqlParameter[] pram =
            {
                new SqlParameter("@nodeID",SqlDbType.Int,0),
            };
            pram[0].Value = _nodeID;
            return RunDview(Sqlstr(2), pram);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            ManBind();
        }
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            MessageBox.Show(e.Node.Tag.ToString());
        }   
    }

    //实体
    public class Treev
    {
        private int cid = 0;
        private string cname = "";
        private int cparentid = 0;
        public Treev()
        {

        }
        public int Cid
        {
            get { return cid; }
            set { cid = value; }
        }
        public string Cname
        {
            get { return cname; }
            set { cname = value; }
        }
        public int Cparentid
        {
            get { return cparentid; }
            set { cparentid = value; }
        }
    }

}

 

转载于:https://www.cnblogs.com/orc2/archive/2012/11/21/2781352.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值