首页 > 开发 > 综合 > 正文

解决了,通过DataSet的递归操作TreeView生成树状图

2024-07-21 02:27:54
字体:
来源:转载
供稿:网友
从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 ,
)


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表