Display Hierarchical Data with TreeView

xiaoxiao2021-03-05  19

Display Hierarchical Data with TreeView

TEEMU Keiski

Very often applications have need to manage hierarchical data and indeed such that hierarchy is unlimited. This has been a bit problem in databases, although solved using self-referencing tables or alternative solutions. OK, but how to display such data that uses self-referencing TABLE?

With ASP.NET one optional answer is to use TreeView control. TreeView gives developers chance to display hierarchical data as I'll demonstrate in this article. What makes TreeView so cool is that it can also display elements (tree nodes) based on XML.

Overview of things that developer Needs to do to get this Feature Are:

Get The Data from Self-Referencing Table Into DataSet

Create 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 That In Database We Have Table as Follows:

Categories

CategoryID

ParentcategoryID

CategoryName

2

1 3

twenty four

3 5 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.11 16 14 3.1. 1.1.2 17 14 3.1.1.1.3

Shortly, categoryid is the primary key and petcategoryid is foreign key referencing to categoryid with default value null. CategoryName Represents text i Want To Display In TreeView.

Get Data Into DataSet

// Connection to Database

OLEDBCONNECTION OBJCONN = New OLEDBCONNECTION ("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 = new oledbcommand ("Select * from categories", objconn;

// OLEDBDataAdapter

OLEDBDataAdapter objda = new oledbdataadapter (Objcommand);

// dataset

Dataset DS = New Dataset ("CategoriesDS");

// Fill DataSet

Objda.Fill (DS, "categories");

Nested DataRelation

// CREATE DATARELATION

Dataralation Drel = New DataRelation ("categories_recursive", ds.tables ["categories"]. Columns ["categoryid"], ds.tables ["categories"]. Column ["parentcategoryID"]);

// Make Sure Relation IS NESTED

Drel.NESTED = TRUE;

// Add rate to DataSet's Relations Collection

DS.RELATIONS.ADD (DREL);

DataSet's XML and Xslt Transformation [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 ();

// loading xslt stylesheet

Objxsl.Load (Server.Mappath ("TransformationTemplate.xslt");

// StringWriter to Temporarily Hold Result of the Transformation

StringWriter Writer = new stringwriter ();

// Apply Transformation with no arguments and dump results to stringwriter.

Objxsl.Transform (Objdoc.createnavigator (), null, write);

Bind TreeView [Result of Transformation]

// set Treeview's Treenodesrc property to get XML from stringwriter.

TreeView1.treenodesrc = Writer.toString ();

// Bind TreeView

TreeView1.databind ();

// close stringwriterwriter.close ();

Other Useful Resources at Aspalliance:

Programming with treeview by Steve Sharock

Developing with the treeview Web Control Part 1 by James Avery

Developing with the treeview web control part 2 by James avery

转载请注明原文地址:https://www.9cbs.com/read-39106.html

New Post(0)