TreeView绑定数据库(转贴+自己的心得)

TreeView是一个很好的控件,现在看见好多项目都用到它,它可以实现mmc那种操作界面。

http://msdn.microsoft.com/archive/en-us/samples/internet/asp_dot_net_servercontrols/webcontrols/default.asp提供下载

在安装时要注意的问题

1进入.net命令提示运行.bat文件

2把文件夹webctrl_client放在网站的根目录下

3在.net中添加引用Microsoft.Web.UI.WebControls.dll

这是我一次安装出错时的发帖(http://community.csdn.net/Expert/topic/4158/4158536.xml?temp=.2670099

在微软的

http://www.microsoft.com/china/msdn/archives/library/workshop/webcontrols/overview/treeview.asp

全面介绍了TreeView的用法

现在我说一下绑定数据库

参照(http://www.microsoft.com/china/community/Column/30.mspx)

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)
**********************************************************************

数据绑定

public class Index : System.Web.UI.Page
 {    DataSet ds=new DataSet();
  protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
   
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
            SqlConnection CN = new SqlConnection();
   try
   {
    //初始化连接字符串
    CN.ConnectionString="server=.;uid=sa;pwd=sa;database=Test";
    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)
   {
    throw (ex);  
   }
   finally
   {
    CN.Close();
   }
   //调用递归函数,完成树形结构的生成
   //tn2=AddTree(0, (TreeNode)null);
   //TreeView1.Nodes.Add(tn2);
         AddTree(0, (TreeNode)null);

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

我的是.net2003我

参照

http://www.microsoft.com/china/community/Column/30.mspx

我的.net报错"Node只能在一个数据集"

所以我改为上面的代码就没报错

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值