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:

  1. Get the data from self-referencing table into DataSet
  2. 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.
  3. Get DataSet’s XML representation and apply XSLT transformation for it so that result corresponds to XML syntax used by TreeView control.
  4. Bind TreeView.

Self-referencing table

Assume that in database we have table as follows:

CATEGORIES

CategoryID

ParentCategoryID

CategoryName

2

1

3

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!