Click here to Skip to main content
15,896,154 members
Articles / Web Development / ASP.NET

Implementing IHierarchy Support Into Your Custom Collections

Rate me:
Please Sign up or sign in to vote.
4.85/5 (25 votes)
17 Jul 20073 min read 158.6K   1.9K   67  
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) {
					children.Add(category);
				}
			}

			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(); }
		}

		#endregion

		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.

License

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


Written By
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 (http://www.simplifyitwebdesign.com). 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

Comments and Discussions