Click here to Skip to main content
15,895,142 members
Articles / Web Development / HTML

Binding the ASP.NET TreeView to a DataSet or an ObjectDataSource

Rate me:
Please Sign up or sign in to vote.
4.70/5 (17 votes)
1 May 2008CPOL1 min read 150.2K   4.1K   32  
The TreeView can not bind to a DataSet or to an ObjectDataSource. With one line of code, you can do that now.
using System;
using System.Data;
using System.Web.UI;
using System.Collections;

/*************************************************************************/
/*   Written By Ralph Varjabedian                                        */
/*   You may use this code freely and copy this code provided that       */
/*   You do not remove this copyright notice.                            */
/*   April 2008                                                          */
/*   http://www.varjabedian.net/archive/2008/04/22/binding-asp.net-treeview-to-a-dataset-or-an-objectdatasource.aspx */
/*************************************************************************/

namespace TreeViewBindingTest
{
    /// <summary>
    /// A class that translates a DataSet into IHierarchicalDataSource that can be used to bind Hierarchical data to a TreeView
    /// </summary>
    public class HierarchicalDataSet : IHierarchicalDataSource
    {
        DataSet dataSet;
        string idColumnName;
        string parentIdColumnName;

        /// <summary>
        /// The constructor of the class
        /// </summary>
        /// <param name="dataSet">The dataset that contains the data</param>
        /// <param name="idColumnName">The Primary key column name</param>
        /// <param name="parentidColumnName">The Parent Primary key column name that identifies the Parent-Child relationship</param>
        public HierarchicalDataSet(DataSet dataSet, string idColumnName, string parentIdColumnName)
        {
            this.dataSet = dataSet;
            this.idColumnName = idColumnName;
            this.parentIdColumnName = parentIdColumnName;
        }

        public event EventHandler DataSourceChanged; // never used here

        public HierarchicalDataSourceView GetHierarchicalView(string viewPath)
        {
            return new DataSourceView(this, viewPath);
        }

        #region supporting methods
        DataRowView GetParentRow(DataRowView row)
        {
            dataSet.Tables[0].DefaultView.RowFilter = String.Format("{0} = {1}", idColumnName, row[parentIdColumnName].ToString());
            DataRowView parentRow = dataSet.Tables[0].DefaultView[0];
            dataSet.Tables[0].DefaultView.RowFilter = "";
            return parentRow;
        }

        string GetChildrenViewPath(string viewPath, DataRowView row)
        {
            return viewPath + "\\" + row[idColumnName].ToString();
        }

        bool HasChildren(DataRowView row)
        {
            dataSet.Tables[0].DefaultView.RowFilter = String.Format("{0} = {1}", parentIdColumnName, row[idColumnName]);
            bool hasChildren = dataSet.Tables[0].DefaultView.Count > 0;
            dataSet.Tables[0].DefaultView.RowFilter = "";
            return hasChildren;
        }

        string GetParentViewPath(string viewPath)
        {
            return viewPath.Substring(0, viewPath.LastIndexOf("\\"));
        }
        #endregion

        #region private classes that implement further interfaces
        class DataSourceView : HierarchicalDataSourceView
        {
            HierarchicalDataSet hDataSet;
            string viewPath;

            public DataSourceView(HierarchicalDataSet hDataSet, string viewPath)
            {
                this.hDataSet = hDataSet;
                this.viewPath = viewPath;
            }

            public override IHierarchicalEnumerable Select()
            {
                return new HierarchicalEnumerable(hDataSet, viewPath);
            }
        }

        class HierarchicalEnumerable : IHierarchicalEnumerable
        {
            HierarchicalDataSet hDataSet;
            string viewPath;

            public HierarchicalEnumerable(HierarchicalDataSet hDataSet, string viewPath)
            {
                this.hDataSet = hDataSet;
                this.viewPath = viewPath;
            }

            public IHierarchyData GetHierarchyData(object enumeratedItem)
            {
                DataRowView row = (DataRowView)enumeratedItem;
                return new HierarchyData(hDataSet, viewPath, row);
            }

            public IEnumerator GetEnumerator()
            {
                if (viewPath == "")
                    hDataSet.dataSet.Tables[0].DefaultView.RowFilter = String.Format("{0} is null", hDataSet.parentIdColumnName);
                else
                {
                    string lastID = viewPath.Substring(viewPath.LastIndexOf("\\") + 1);
                    hDataSet.dataSet.Tables[0].DefaultView.RowFilter = String.Format("{0} = {1}", hDataSet.parentIdColumnName, lastID);
                }

                IEnumerator i = hDataSet.dataSet.Tables[0].DefaultView.GetEnumerator();
                hDataSet.dataSet.Tables[0].DefaultView.RowFilter = "";
                return i;
            }
        }

        class HierarchyData : IHierarchyData
        {
            HierarchicalDataSet hDataSet;
            DataRowView row;
            string viewPath;

            public HierarchyData(HierarchicalDataSet hDataSet, string viewPath, DataRowView row)
            {
                this.hDataSet = hDataSet;
                this.viewPath = viewPath;
                this.row = row;
            }

            public IHierarchicalEnumerable GetChildren()
            {
                return new HierarchicalEnumerable(hDataSet, hDataSet.GetChildrenViewPath(viewPath, row));
            }

            public IHierarchyData GetParent()
            {
                return new HierarchyData(hDataSet, hDataSet.GetParentViewPath(viewPath), hDataSet.GetParentRow(row));
            }

            public bool HasChildren
            {
                get
                {
                    return hDataSet.HasChildren(row);
                }
            }

            public object Item
            {
                get
                {
                    return row;
                }
            }

            public string Path
            {
                get
                {
                    return viewPath;
                }
            }

            public string Type
            {
                get
                {
                    return typeof(DataRowView).ToString();
                }
            }
        }
        #endregion
    }
}

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
Technical Lead
Lebanon Lebanon
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions