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.
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
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
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.