• Now Online : 32
  • admin@codemyne.net

Introduction

Treeview provides a hierarchical view that is similar to a directory tree displayed by windows explorer. Some times, it requires to populate the treeview dynamically through database. In this article, an example is given how to populate treeview dynamically.

Start a new website. And take a treeview control into the div tag

    private void PopulateTreeview()
    {
    try
        {
            DataTable dtparent = new DataTable();
            DataTable dtchild = new DataTable();
            dtparent = FillParentTable();
            dtchild = FillChildTable();
            DataSet ds = new DataSet();
            ds.Tables.Add(dtparent);
            ds.Tables.Add(dtchild);
            ds.Relations.Add("children", dtparent.Columns["ParentId"], dtchild.Columns["ParentId"]);
            if (ds.Tables[0].Rows.Count > 0)
            {
                TreeView1.Nodes.Clear();
                
                foreach (DataRow masterRow in ds.Tables[0].Rows)
                {
                    TreeNode masterNode = new TreeNode((string)masterRow["ParentName"], Convert.ToString(masterRow["ParentId"]));
                    TreeView1.Nodes.Add(masterNode);
                    foreach (DataRow childRow in masterRow.GetChildRows("Children"))
                    {
                        TreeNode childNode = new TreeNode((string)childRow["ChildName"], Convert.ToString(childRow["ParentId"]));                       
                        masterNode.ChildNodes.Add(childNode);
                        childNode.Value = Convert.ToString(childRow["ChildId"]);                       
                    }
                }
            }
        }
        catch (Exception ex)
        {
        throw new Exception("Unable to populate treeview" + ex.Message);
        }
    }
    private DataTable FillChildTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ChildId", Type.GetType("System.Int32"));
        dt.Columns.Add("ParentId", Type.GetType("System.Int32"));
        dt.Columns.Add("ChildName", Type.GetType("System.String"));
        DataRow dr;
        dr = dt.NewRow();
        dr[0] = 1;
        dr[1] = 1;
        dr[2] = "Manager";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = 2;
        dr[1] = 1;
        dr[2] = "Executive";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = 3;
        dr[1] = 2;
        dr[2] = "Analyst";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = 4;
        dr[1] = 2;
        dr[2] = "Manager";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = 5;
        dr[1] = 3;
        dr[2] = "Programmer";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = 6;
        dr[1] = 3;
        dr[2] = "Team Leader";
        dt.Rows.Add(dr);
        return dt;
    }
    private DataTable FillParentTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ParentId", Type.GetType("System.Int32"));
        dt.Columns.Add("ParentName",Type.GetType("System.String"));
        DataRow dr;
        dr = dt.NewRow();
        dr[0] = 1;
        dr[1] = "Markettng";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = 2;
        dr[1] = "Production";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = 3;
        dr[1] = "Systems";
        dt.Rows.Add(dr);
        return dt;
    }

Comments Post a Comment

ashish 1/30/2011 (IST) / Reply

i am mca student please help me to devlop the project

surekha 3/29/2011 (IST) / Reply

very good for any person who first time using treeview. thanks

Sankar 5/7/2011 (IST) / Reply

i try put i didn't receive the text in the log page

Arjunan 3/6/2013 (IST) / Reply

Hi..Code snippets are nice.Please add comments and Images if possible.This could help readers to understand it easily.

mamta 4/14/2013 (IST) / Reply

nice project

vatsala 5/6/2013 (IST) / Reply

awsum ...!!!

lin Pha 8/3/2013 (IST) / Reply

Nice

blackEagle 9/25/2014 (IST) / Reply

hey man, why is it that the zip is not opening on all PCs I have on my disposal!! I wanted to study your code bt failed to open

Admin 10/11/2014 (IST) / Reply

@blackEagle: Try to open the file in WinRAR. It is working fine.

hanmant 3/3/2015 (IST) / Reply

i want code for blood bank web site

Dileep Soni 8/27/2015 (IST) / Reply

i like it