Click here to Skip to main content
15,891,253 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.4K   1.9K   67  
Brief walk-through on decorating your custom collections with the IHierarchy family of interfaces to support databinding.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace HierarchyExample {

	public class Common {

		private static CategoryCollection collection;

		private Common() {
		}

		/// <summary>
		/// Wraper around local data cache to retrieve just root categories from the collection
		/// </summary>
		/// <returns>CategoryCollection containing just categories with a parentId of "root"</returns>
		public static CategoryCollection GetRootCategories() {
			CategoryCollection rootCategories = new CategoryCollection();
			foreach (Category category in GetCategoryData()) {
				if (category.ParentId == 0)
					rootCategories.Add(category);
			}
			return rootCategories;
		}

		/// <summary>
		/// Method to generate sample data for examples
		/// </summary>
		/// <returns>CategoryCollection containing computer store related categories.</returns>
		public static CategoryCollection GetCategoryData() {

			// Simulate going to the database or pulling from a local cache
			if (collection == null) {

				collection = new CategoryCollection();
				collection.Add(new Category(1, 0, "Computer Cases"));
				collection.Add(new Category(2, 0, "Hard Drives"));
				collection.Add(new Category(3, 0, "Memory"));
				collection.Add(new Category(4, 0, "Input Devices"));
				collection.Add(new Category(5, 0, "Monitors"));

				collection.Add(new Category(10, 1, "Case Accessories"));
				collection.Add(new Category(11, 1, "Computer Cases"));
				collection.Add(new Category(12, 1, "External Enclosures"));
				collection.Add(new Category(13, 1, "Server Chassis"));

				collection.Add(new Category(20, 2, "Internal Hard Drives"));
				collection.Add(new Category(21, 2, "Laptop Hard Drives"));
				collection.Add(new Category(22, 2, "Network Hard Drives"));

				collection.Add(new Category(30, 3, "Desktop Memory"));
				collection.Add(new Category(31, 3, "Laptop Memory"));
				collection.Add(new Category(32, 3, "Server Memory"));

				collection.Add(new Category(40, 4, "Keyboards"));
				collection.Add(new Category(41, 4, "Mouse"));
				collection.Add(new Category(42, 4, "Tablets"));
				collection.Add(new Category(43, 4, "Web Cams"));

				collection.Add(new Category(50, 5, "CRT Monitors"));
				collection.Add(new Category(51, 5, "LCD Monitors"));
				collection.Add(new Category(52, 5, "Montior Accessories"));
				collection.Add(new Category(53, 5, "Touchscreen Monitors"));

				collection.Add(new Category(60, 30, "168-Pin SDRAM"));
				collection.Add(new Category(61, 30, "184-Pin DDR SDRAM"));
				collection.Add(new Category(62, 30, "184-Pin RDRAM (16bit)"));
				collection.Add(new Category(63, 30, "240-Pin DDR2 SDRAM"));
				collection.Add(new Category(64, 30, "240-Pin DDR3 SDRAM"));

				collection.Add(new Category(70, 63, "DDR2 400 (PC2 3200)"));
				collection.Add(new Category(71, 63, "DDR2 533 (PC2 4200)"));
				collection.Add(new Category(72, 63, "DDR2 533 (PC2 4300)"));
				collection.Add(new Category(73, 63, "DDR2 667 (PC2 5300)"));
				collection.Add(new Category(74, 63, "DDR2 667 (PC2 5400)"));
				collection.Add(new Category(75, 63, "DDR2 800 (PC2 6400)"));

			}

			return collection;

		}

	}

}

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