TreeView 绑定SQL
<div id="cnblogs_post_body">代码复制粘贴就能运行。<div class="cnblogs_Highlighter">create database CityDBgouse CityDBgocreate table CityTB( cid int primary key not null,--编号 cname varchar(10) not null,--名称 cparentID int not null --父节点,对应编号);goselect * from CityTBinsert 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.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.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; } } }}
页:
[1]