Click here to Skip to main content
15,891,828 members
Articles / Web Development / HTML

Templated Hierarchical Repeater Control

Rate me:
Please Sign up or sign in to vote.
4.12/5 (10 votes)
5 Mar 2009CPOL7 min read 45.7K   1K   32  
A Templated Databound control for making TreeViews and displaying hierarchical data
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Web.UI;

namespace DaveControls.HierarchicalControls.Data
{
    public class HierarchyDataHelper
    {
        private const char NODE_CHAR = '.';

        internal static HierarchyDataItemBase CreateDummyDataSource(string dataHierarchy)
        {
            string workingString = dataHierarchy;

            HierarchyDataItemBase dataSource = new HierarchyDataItemBase();

            HierarchyDataItemBase currentParent = dataSource;

            for (int i = 0; i < dataHierarchy.Length; i++)
            {
                if (dataHierarchy[i] == NODE_CHAR)
                {
                    HierarchyDataItemBase newNode = new HierarchyDataItemBase();

                    currentParent.Add(newNode);
                }
                else if (dataHierarchy[i] == '(')
                {
                    // we're going down a level.  Set the current parent to be the last node that was added
                    currentParent = currentParent[currentParent.Count - 1];
                }
                else if (dataHierarchy[i] == ')')
                {
                    // we're going up a level. Set the current parent to be the parent of the last item that was added
                    if (currentParent.Parent == null)
                    {
                        // we are back to the root level again
                        currentParent = dataSource;
                    }
                    else
                    {
                        currentParent = currentParent.Parent;
                    }
                }
            }

            return dataSource;
        }

        internal static string CreateDataHierarchyString(HierarchyDataItemBase dataSource)
        {
            IEnumerator<HierarchyDataItemBase> dataEnum = (IEnumerator<HierarchyDataItemBase>)dataSource.GetEnumerator();

            StringBuilder sb = new StringBuilder();

            while (dataEnum.MoveNext())
            {
                HierarchyDataItemBase data = dataEnum.Current;

                sb.Append(NODE_CHAR);

                if (data.HasChildren)
                {
                    sb.Append("(");
                    sb.Append(CreateDataHierarchyString(data));
                    sb.Append(")");
                }
            }

            return sb.ToString();
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions