日期:2011-03-01  浏览次数:20422 次

从csdn上问来的,谢谢stdao(道可道) 老弟,呵呵。

using Microsoft.Web.UI.WebControls;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace test
{
public class WebForm1 : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView tv;

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{bindtree(tv.Nodes,"0");}
}
private void bindtree(TreeNodeCollection Nds , string depid)
{
DataSet ds=bindds();//获取DataSet,具体代码略
DataView dv=ds.Tables["tree"].DefaultView;
dv.RowFilter="HiDepID="+depid;
TreeNode tn;
string strID;
foreach(DataRowView dr in dv)
{
  strID=dr["DepID"].ToString();
  if (strID!="")
  {
   tn=new TreeNode();
   tn.ID=dr["DepID"].ToString();
   tn.Text=dr["Name"].ToString();
   Nds.Add(tn);
   bindtree(Nds[Nds.Count-1].Nodes,strID);
  }
}
}
}
}

数据库结构:
CREATE TABLE [dbo].[tbDep] (
    [DepID] [smallint] IDENTITY (1, 1) NOT NULL ,
    [DepName] [varchar] (50) NOT NULL ,
    [HiDepID] [smallint] NULL ,
)