Display Hierarchical data with TreeView

Source: Internet
Author: User
Tags bind xslt
TreeView Display Hierarchicaldata with Treeviewteemu Keiski


Very Oftenapplications have need to manage hierarchical data and indeed such-is thathierarchy. This is has been a bit problem in databases, althoughsolved using self-referencing tables or alternative solutions. OK, but how todisplay such the data that uses self-referencing table?



Withasp.net One optional answer is to use the TreeView control. TreeView givesdevelopers chance to display hierarchical data as I ' ll demonstrate in thisarticle. What makes TreeView So, it can also display elements (TreeNodes) based on XML.



Overview ofthings that developer needs to does to get this feature are:
Get the "Data from self-referencing table" into Datasetcreate DataRelation, corresponds to relation in self-referencing Table and add it to DataSet ' s Relations collection. DataRelation ' s Nested property needs to is true. Get DataSet ' s XML representation and apply XSLT transformation for it, so, corresponds to XML syntax used by Tre Eview 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 are foreign key referencingto 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 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 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.