Monday, August 4, 2014

Create Dynamic TreeView from DataTable in C#.Net





using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class DynamicTreeView : System.Web.UI.Page
{
    TreeNode tn;
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateDynamicTreeView();
    }

    private void CreateDynamicTreeView()
    {
        try
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MenuID");
            dt.Columns.Add("MenuName");
            dt.Columns.Add("ParentID");


            DataRow dr = dt.NewRow();
            dr["MenuID"] = "0";
            dr["MenuName"] = "Earth";
            dr["ParentID"] = "Null";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["MenuID"] = "1";
            dr["MenuName"] = "Europe";
            dr["ParentID"] = "0";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["MenuID"] = "2";
            dr["MenuName"] = "Asia";
            dr["ParentID"] = "0";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["MenuID"] = "3";
            dr["MenuName"] = "Africa";
            dr["ParentID"] = "0";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["MenuID"] = "4";
            dr["MenuName"] = "Germany";
            dr["ParentID"] = "1";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["MenuID"] = "5";
            dr["MenuName"] = "France";
            dr["ParentID"] = "1";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["MenuID"] = "6";
            dr["MenuName"] = "China";
            dr["ParentID"] = "2";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["MenuID"] = "7";
            dr["MenuName"] = "India";
            dr["ParentID"] = "2";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["MenuID"] = "8";
            dr["MenuName"] = "UAE";
            dr["ParentID"] = "2";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["MenuID"] = "9";
            dr["MenuName"] = "Egypt";
            dr["ParentID"] = "3";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["MenuID"] = "10";
            dr["MenuName"] = "Libya";
            dr["ParentID"] = "3";
            dt.Rows.Add(dr);


            foreach (DataRow dr1 in dt.Select("ParentID = 'Null'"))
            {
                TreeView1.Nodes.Add(new TreeNode(dr1["MenuName"].ToString(), dr1["MenuID"].ToString()));
            }

            foreach (DataRow dr2 in dt.Select("ParentID <> 'Null'"))
            {
                TreeNode tnd = new TreeNode(dr2["MenuName"].ToString(), dr2["MenuID"].ToString());
                TraverseTree(TreeView1.Nodes, dr2["ParentID"].ToString());

                tn.ChildNodes.Add(tnd);
            }
        }
        catch (Exception ex)
        {
            String strErr = ex.Message.ToString();
        }
    }

    void TraverseTree(TreeNodeCollection nodes, String strVal)
    {
        foreach (TreeNode child in nodes)
        {
            if (child.Value == strVal)
            {
                tn = child;
            }
            else
            {
                TraverseTree(child.ChildNodes, strVal);
            }
        }
    }
}

No comments:

Post a Comment