新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > .Net开发 > Asp.net教程 > 正文:Display hierarchical data with TreeView

Display hierarchical data with TreeView

新客网 XKER.COM 2005-05-12 来源: 收藏本文
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
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐