从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 ,
)
