国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > 綜合 > 正文

Display hierarchical data with TreeView

2024-07-21 02:15:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
display hierarchicaldata with treeviewteemu keiski
 

very oftenapplications have need to manage hierarchical data and indeed such thathierarchy is unlimited. this has been a bit problem in databases, althoughsolved using self-referencing tables or alternative solutions. ok, but how todisplay such data that uses self-referencing table?

 

withasp.net one optional answer is to use treeview control. treeview givesdevelopers chance to display hierarchical data as i’ll demonstrate in thisarticle. what makes treeview so cool is that it can also display elements (treenodes) based on xml.

 

overview ofthings that developer needs to do to get this feature are:
get the data from self-referencing table into datasetcreate datarelation that corresponds to relation in self-referencing table and add it to dataset’s relations collection. datarelation’s nested property needs to be true.get dataset’s xml representation and apply xslt transformation for it so that result corresponds to xml syntax used by treeview control.bind treeview.
 
self-referencing table
assume thatin database we have table as follows:

categories

categoryid

parentcategoryid

categoryname

2

 

1

3

 

2

4

 

3

5

2

1.1

6

2

1.2

7

2

1.3

8

3

2.1

9

3

2.2

10

4

3.1

11

5

1.1.1

12

5

1.1.2

13

10

3.1.1

14

13

3.1.1.1

15

14

3.1.1.1.1

16

14

3.1.1.1.2

17

14

3.1.1.1.3

 

shortly,categoryid is the primary key and parentcategoryid is foreign key referencingto categoryid with default value null. categoryname represents text i want todisplay in treeview.

 
get data into dataset
//connection to database

oledbconnection objconn=newoledbconnection("provider=microsoft.jet.oledb.4.0;data source=" +server.mappath("db.mdb")  +";persist security info=false");

 

//sql query to get data from categories table

oledbcommand objcommand=newoledbcommand("select * from categories",objconn);

 

//oledbdataadapter

oledbdataadapter objda =newoledbdataadapter(objcommand);

 

//dataset

dataset ds=newdataset("categoriesds");

 

//fill dataset

objda.fill(ds ,"categories");

 
nested datarelation
//create datarelation

datarelation drel=newdatarelation("categories_recursive",
ds.tables["categories"].columns["categoryid"],
ds.tables["categories"].columns["parentcategoryid"]);

 

//make sure relation is nested

drel.nested =true;

 

//add relation to dataset's relations collection

ds.relations.add(drel);

 
dataset’s xml and xslttransformation [source xml][xslt stylesheet]
//xmldocument to hold xml generated from dataset

xmldocument objdoc=new xmldocument();

 

//load xml

objdoc.loadxml(ds.getxml());

 

//create xsltransform object

xsltransform objxsl=new xsltransform();

 

//load xslt stylesheet

objxsl.load(server.mappath("transformationtemplate.xslt"));

 

//stringwriter to temporarily hold result of thetransformation

stringwriter writer=new stringwriter();

 

//apply transformation with no arguments and dumpresults to stringwriter.

objxsl.transform(objdoc.createnavigator(),null,writer);

 
bind treeview [result of transformation] 
//set treeview's treenodesrc property to get xml fromstringwriter.

treeview1.treenodesrc =writer.tostring();

 

//bind treeview

treeview1.databind();

 

//close stringwriter

writer.close();


other useful resources at aspalliance:
programming with treeview by steve sharrock
developing with the treeview web control part 1 by james avery
developing with the treeview web control part 2 by james avery
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 柘荣县| 钦州市| 常熟市| 阜宁县| 偃师市| 兴隆县| 中山市| 河东区| 突泉县| 信丰县| 金堂县| 师宗县| 儋州市| 岗巴县| 商洛市| 剑川县| 澄江县| 霍山县| 杭锦后旗| 桦川县| 长阳| 高邑县| 鄂州市| 五寨县| 荣成市| 南漳县| 科技| 赞皇县| 定兴县| 大邑县| 五华县| 佛坪县| 迭部县| 延安市| 雷波县| 九龙坡区| 麻城市| 洛隆县| 洛隆县| 林周县| 宝鸡市|