Display hierarchical data with TreeView

treeview

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

时间: 2025-01-29 07:53:27

Display hierarchical data with TreeView的相关文章

Hierarchical Data Format 1.9.114发布 科学数据存储的文件格式

Hierarchical Data Format(简称HDF)是一个通用库,用于存储科学数据的文件格式.它可以将http://www.aliyun.com/zixun/aggregation/14345.html">数据处理到数据集(多维数组)和群组(组织对象结构). Hierarchical Data Format 1.9.114该版本更新了新API调用的Fortran打包.修复了一个超大的数据筛缓冲区.一些轻微错误修正. 软件信息:http://www.hdfgroup.org/HDF

Hierarchical Data Format 1.9.115发布 科学数据存储的文件格式

Hierarchical Data Format(简称HDF)是一个通用库,用于存储科学数据的文件格式.它可以将http://www.aliyun.com/zixun/aggregation/14345.html">数据处理到数据集(多维数组)和群组(组织对象结构). Hierarchical Data Format 1.9.115该版本在h5unjam,1 sefgault时使用-V(演出版)选项已修复.在h5repack,改变尺寸无限的最大数量的块大小指定区块集时失败已修复. 软件信息

Hierarchical Data Format 1.9.89发布 存储科学数据

Hierarchical Data Format是一个通用的库和文件格式,用于存储科学数据.它用于排到数据集(多维数组)和组(组织对象的结构)的数据. Hierarchical Data Format 1.9.89该版本专用的Collective I/O和Complex Derived Datatype MPI功能现在始终处于启用状态.1.6版修补程序的向后兼容.Windows兼容性修复.改进完整性检查工具. 下载地址: hdf5-1.9.89.tar .bz2 28-Aug-2011 09:2

Hierarchical Data Format 1.9.110发布 科学数据存储的文件格式

Hierarchical Data Format(简称HDF)是一个通用库,用于存储科学数据的文件格式.它可以将http://www.aliyun.com/zixun/aggregation/14345.html">数据处理到数据集(多维数组)和群组(组织对象结构). Hierarchical Data Format 1.9.110该版本清除了构建系统,增加了Linux并行版本的默认,重构和加速处理大型数据集的代码,修正后为对象的符号链接,并增加了配套轻微错误修正. 软件信息:http:/

ASP.NET 嵌套Repeater

asp.net 本来不怎么用Repeater的,可最近要显示留言板一样的东西,用Grid实现不好看,Grid比较适合正规的WEB系统,对于网站类型的,还是用Repeater了,遇到需要嵌套Repeater的情况,就收藏一下,以后可能还会用到哦.原文来自MS:http://support.microsoft.com/default.aspx?scid=kb;en-us;306154 iew products that this article applies to.Article ID : 306

Posting form data from ASP.NET page to another URL

asp.net source: http://www.c-sharpcorner.com/Code/2004/Sept/ASP.NetPostURL.asp begin: AuthorDate Of SubmissionUser LevelJigar Desai09/27/2004IntermediateDownload: Remote Post 2Kb IntroductionSometime you need to post a form to an different url from a

treeview-bootstrap treeView 如何追加节点

问题描述 bootstrap treeView 如何追加节点 bootstrap treeView 如何追加节点?看了api和其他demo没发现有增加节点的方法 解决方案 bootstrap 没有,bootstrap 只是提供一些样式,你的tree是怎么形成的,如果是第三方插件,插件中应该有对tree的操作,否则你就用JavaScript操作吧 解决方案二: var tree = [ { text: ""Parent 1"" nodes: [ { text: &qu

基于MVC5和Bootstrap的jQuery TreeView树形控件(一)之数据支持json字符串、list集合_jquery

本文支持两种方式的数据,一种为List集合,一种为json字符串. 先来介绍一下后台返回list集合(推荐使用此方法): 控制器代码如下: public static List<TC_DictionaryInfo> DInfo = new List<TC_DictionaryInfo>(); /// <summary> /// TreeView视图 /// </summary> /// <returns></returns> publ

Bootstrap树形菜单插件TreeView.js使用方法详解_javascript技巧

jQuery多级列表树插件基于Twitter Bootstrap,以简单和优雅的方式来显示一些继承树结构,如视图树.列表树等等. 实用Bootstrap树形菜单特效插件Bootstrap Tree View,非常不错的Bootstrap插件,现在很多Bootstrap制作的页面都需要此功能,此插件需要Bootstrap3版本以及jQuery 2.0极以上版本支持,支持众多参数自定义功能,颜色.背景色.图标.链接等,还是很不错的. 效果图: 具体使用方法: 插件依赖 Bootstrap v3.0.