国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 永兴县| 巫溪县| 繁峙县| 扎鲁特旗| 奉节县| 繁昌县| 鹰潭市| 英吉沙县| 闽侯县| 安溪县| 丹阳市| 诸暨市| 平江县| 南宁市| 新河县| 准格尔旗| 江陵县| 宁化县| 集安市| 刚察县| 泾阳县| 青海省| 小金县| 晋宁县| 三河市| 集安市| 丰宁| 汉中市| 余庆县| 宣汉县| 忻城县| 惠州市| 清原| 上思县| 阿瓦提县| 曲靖市| 武邑县| 京山县| 博湖县| 凌海市| 吴旗县|