Home » Visual Studio

How to add n-number of nodes in Treeview using DataSet

Hi Everyone,
I am Using Treeview Control in my application. My all menue detail is available in Db and i am getting it in dataset. I am binding my TreeviewControl with Dataset and it shows all nodes properly. I am able to done this at 2 levels. But i can't do this work at n-number of nodes. can anybody help me that how i can do this for n-number of nodes using Dataset. Waiting for quich Response. Thanks in Advance.


3 Answers Found


Answer 2

Please use ParentID, ID relationship. I have built the data table in the code but you may get the data table from the database.



<%@ Page Language="C#"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script runat="server">
        protected override void OnLoad(EventArgs e)
            if (!this.IsPostBack)
                this.AddNodes(this.trvCategories.Nodes, 0, this.LoadData());
        private void AddNodes(TreeNodeCollection nodes, int level, System.Data.DataTable dt)
            string filterExp = string.Format("ParentID='{0}'", level);
            foreach (System.Data.DataRow r in dt.Select(filterExp))
                TreeNode item = new TreeNode()
                    Text = r[1].ToString(),
                    Value = r[2].ToString()
                this.AddNodes(item.ChildNodes, int.Parse(r[0].ToString()), dt);
        private System.Data.DataTable LoadData()
            /// For the simplicity I do build the datatable dynamically,
            /// But you may get this data from the DATABASE
            System.Data.DataTable dt = new System.Data.DataTable();
            int index = 9;
            for (int i = 0; i < 1100; i++)
                string item = "{0},Item{1},Value{1},{2},{1}";
                if (i < 110)
                    index = i < 10 ? 0 : int.Parse(Math.Ceiling((decimal)i / 10).ToString());
                    dt.Rows.Add((string.Format(item, i + 1, i, index)).Split(char.Parse(",")));
                    if (i % 10 == 0) index++;
                    dt.Rows.Add((string.Format(item, i + 1, i, index)).Split(char.Parse(",")));
            return dt;
    <form id="form1" runat="server">
        <asp:TreeView runat="server" ID="trvCategories" />

Answer 3

Hi Everyone,

I've solved problem for adding node at nth level. This this is possible by using Recursive call of Adding Child node methods. Below is the sample code for adding n-number  of node in Treeview using DatSet.

foreach (DataRow dr in dsEntity.Tables[0].Select("Level = 1"))
                        root = new TreeNode(dr["Name"].ToString(), dr["ID"].ToString());

                        DataRow[] drChild = dsEntity.Tables[0].Select("ParentID = " + dr["ID"].ToString());

                        AddChildNode(dsEntity, drChild, root);

public void AddChildNode(DataSet ds, DataRow[] dr, TreeNode node)

        foreach (DataRow item in dr)
            TreeNode childNode = new TreeNode(item["Name"].ToString(), item["ID"].ToString());

            DataRow[] child2Rows = ds.Tables[0].Select("ParentID = " + item["ID"].ToString());
                AddChildNode(ds, child2Rows, childNode);




<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure