Click here to Skip to main content
11,926,429 members (56,995 online)
Click here to Skip to main content


67 bookmarked

Implementing IHierarchy Support Into Your Custom Collections

, 17 Jul 2007
Brief walk-through on decorating your custom collections with the IHierarchy family of interfaces to support databinding.
using System;
using System.Web.UI;

namespace HierarchyExample {

	public class Category : IHierarchyData {

		private int _categoryId;
		private int _parentId;
		private string _name;

		/// <summary>
		/// Unique identifier for the category
		/// </summary>
		public int CategoryId {
			get { return _categoryId; }
			set { _categoryId = value; }

		/// <summary>
		/// Foreign key to the parent category
		/// </summary>
		public int ParentId {
			get { return _parentId; }
			set { _parentId = value; }

		/// <summary>
		/// Friendly description of the category
		/// </summary>
		public string Name {
			get { return _name; }
			set { _name = value; }

		/// <summary>
		/// Hide the default public constructor
		/// </summary>
		private Category() {

		/// <summary>
		/// Public constructor
		/// </summary>
		/// <param name="categoryId">Unique identifier for the category</param>
		/// <param name="parentId">Foreign key to the parent category</param>
		/// <param name="name">Friendly description of the category</param>
		public Category(int categoryId, int parentId, string name) {
			_categoryId = categoryId;
			_parentId = parentId;
			_name = name;

		#region IHierarchyData Members

		public IHierarchicalEnumerable GetChildren() {

			CategoryCollection children = new CategoryCollection();

			// Loop through your local data and find any children
			foreach (Category category in Common.GetCategoryData()) {
				if (category.ParentId == this.CategoryId) {

			return children;


		public IHierarchyData GetParent() {

			// Loop through your local data and report back with the parent
			foreach (Category category in Common.GetCategoryData()) {
				if (category.CategoryId == this.ParentId)
					return category;

			return null;


		public bool HasChildren {
			get {
				CategoryCollection children = GetChildren() as CategoryCollection;
				return children.Count > 0;

		public object Item {
			get { return this; }

		public string Path {
			get { return this.CategoryId.ToString(); }

		public string Type {
			get { return this.GetType().ToString(); }


		public override string ToString() {
			return this.Name;



By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
Scott Piegdon works full time as a Director of Quality Control for a software development company specializing Credit Union Member Services. He has experience as a Vice President of Information Technologies for a hosted ERP (Enterprise Resource Planning) company. He also runs a Web Design Services company ( He has a beautiful and wonderful wife, and two fantastic children. He loves to develop applications that others find not only easy to use and pleasing to the eye, but also provide un-surpassing functionality. He has been developing websites for over 12 years, and programming professionally for 9 of them. He has worked with dozens of database, scripting, procedural and compiled languages over the years, most recently specializing in Microsoft.Net C# 2.0 with ASP.Net backed by SQL Server 2005.

"Hopefully I can pass along some of the insight and knowledge I have acquired over the years to those curious enough to read it." - Scott Piegdon

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 18 Jul 2007
Article Copyright 2007 by spiegdon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid