TreeView绑定数据库

 

TreeView绑定数据库

1:建立数据库

我们在SQL SERVER 2000里建立一个表tbTree,表的结构设计如下:
列名 数据类型 描述   长度 主键
ID Int 节点编号   4 是
ParentID Int 父节点编号   4
ConText Nvarchar 我们要显示的节点内容 50

在SQL SERVER 2000中建表的脚本:

CREATE TABLE [dbo].[tbTree] (
[ID] [int] NOT NULL ,
[Context] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ParentID] [int] NULL
) ON [PRIMARY]

在表中添加如下记录:

insert tbtree (ID,Context,ParentID) values ( 1,'中国',0)
insert tbtree (ID,Context,ParentID) values ( 2,'北京',11)
insert tbtree (ID,Context,ParentID) values ( 3,'天津',11)
insert tbtree (ID,Context,ParentID) values ( 4,'河北省',1)
insert tbtree (ID,Context,ParentID) values ( 5,'广东省',1)
insert tbtree (ID,Context,ParentID) values ( 6,'广州',5)
insert tbtree (ID,Context,ParentID) values ( 7,'四川省',1)
insert tbtree (ID,Context,ParentID) values ( 8,'成都',7)
insert tbtree (ID,Context,ParentID) values ( 9,'深圳',5)
insert tbtree (ID,Context,ParentID) values ( 10,'石家庄',4)
insert tbtree (ID,Context,ParentID) values ( 11,'辽宁省',1)
insert tbtree (ID,Context,ParentID) values ( 12,'大连',11)
insert tbtree (ID,Context,ParentID) values ( 13,'上海',1)
insert tbtree (ID,Context,ParentID) values ( 14,'天河软件园',6)
insert tbtree (ID,Context,ParentID) values ( 15,'汕头',5)
**********************************************************************

 

ContractedBlock.gif ExpandedBlockStart.gif Code
DataSet ds = new DataSet();
    
protected void Page_Load(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
{
        
if (!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
// 在此处放置用户代码以初始化页面
            SqlConnection CN = new SqlConnection();
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//初始化连接字符串
                CN.ConnectionString = ConfigurationManager.ConnectionStrings["航材库存系统ConnectionString1"].ConnectionString;
                CN.Open();
                
//添加命令,从数据库中得到数据
                SqlCommand sqlCmd = new SqlCommand();
                sqlCmd.Connection 
= CN;
                sqlCmd.CommandText 
= "select * from tbTree";
                sqlCmd.CommandType 
= CommandType.Text;
                SqlDataAdapter adp 
= new SqlDataAdapter(sqlCmd);
                adp.Fill(ds);
            }

            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw (ex);
            }

            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                CN.Close();
            }

            
//调用递归函数,完成树形结构的生成
            
//tn2=AddTree(0, (TreeNode)null);
            
//TreeView1.Nodes.Add(tn2);
            AddTree(0, (TreeNode)null);
        }

}

    
    
public void AddTree(int ParentID, TreeNode pNode)
ExpandedBlockStart.gifContractedBlock.gif    
{
        TreeNode tn1 
= new TreeNode();
        DataView dvTree 
= new DataView(ds.Tables[0]);
        
//过滤ParentID,得到当前的所有子节点
        dvTree.RowFilter = "[PARENTID] = " + ParentID;
        
foreach (DataRowView Row in dvTree)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (pNode == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{    //'?添加根节点
                tn1.Text = Row["ConText"].ToString();
                tn1.Value 
= Row["ID"].ToString();
                TreeView1.Nodes.Add(tn1);
                tn1.Expanded 
= true;
                AddTree(Int32.Parse(Row[
"ID"].ToString()), tn1);    //再次递归
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{   //添加当前节点的子节点
                TreeNode tn2 = new TreeNode();
                tn2.Text 
= Row["ConText"].ToString();
                tn2.Value 
= Row["ID"].ToString();
                
//tn2.NavigateUrl = "javascript:alert('" + Row["ID"].ToString() + "');";
                tn2.ShowCheckBox = true;
                pNode.ChildNodes.Add(tn2);
                tn1.Expanded 
= true;
                AddTree(Int32.Parse(Row[
"ID"].ToString()), tn2);    //再次递归
            }


        }

    }


    
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
{
        Response.Write(
this.TreeView1.SelectedNode.Text);
    }

    
protected void Button1_Click(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
{
        
if (TreeView1.CheckedNodes.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
foreach (TreeNode tn in TreeView1.CheckedNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (tn.ChildNodes.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
foreach (TreeNode tn1 in tn.ChildNodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        Response.Write(tn1.Text 
+ "<br>");
                    }

                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    Response.Write(tn.Text
+"<br>");
                }

            }

        }

    }

 

 

转载于:https://www.cnblogs.com/chnch/articles/1505085.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值