Tree view Data fetch from Database

  • Hi!

    I was trying to created dynamic Tree view in asp.net 2.0 C# .Data Fetch from Database but it expand only two levels . I want multi level Expantion e.g:

    My Data is:

    111

    222

    333

    444

    555

    But it hierachy upto 333 node only I want expand all node is it possible ???

  • First you need to post in the appropriate place. Are you having trouble with the SQL or something else?

    Second, you haven't described what is wrong, what you've done, or where it's not working. From your post it doesn't make sense.

  • Sorry...

    But got solution actually I have problem in ASP.Net

    using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.Data.SqlClient;

    public partial class TreeViewCS : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    ///// if (!Page.IsPostBack)

    //// PopulateRootLevel();

    }

    private void PopulateRootLevel()

    {

    SqlConnection objConn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["Constr"]);

    SqlCommand objCommand = new SqlCommand("select Id_empl,Fullname,(select count(*) FROM master_employee WHERE is_emplid=em.is_emplid) childnodecount FROM master_employee em where is_emplid=(select Id_empl from master_employee where psno= " + txtenter.Text + ")", objConn);

    SqlDataAdapter da=new SqlDataAdapter(objCommand);

    DataTable dt=new DataTable();

    da.Fill(dt);

    PopulateNodes(dt,TreeView1.Nodes);

    }

    private void PopulateSubLevel(int parentid,TreeNode parentNode)

    {

    SqlConnection objConn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["Constr"]);

    SqlCommand objCommand = new SqlCommand(@"select Id_empl,Fullname,(select count(*) FROM master_employee WHERE is_emplid=em.is_emplid) childnodecount FROM master_employee em where is_emplid=@parentID", objConn);

    objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid;

    SqlDataAdapter da=new SqlDataAdapter(objCommand);

    DataTable dt=new DataTable();

    da.Fill(dt);

    PopulateNodes(dt,parentNode.ChildNodes);

    }

    protected void TreeView1_TreeNodePopulate(object sender,TreeNodeEventArgs e)

    {

    PopulateSubLevel(Int32.Parse(e.Node.Value),e.Node);

    }

    private void PopulateNodes(DataTable dt,TreeNodeCollection nodes)

    {

    foreach( DataRow dr in dt.Rows)

    {

    TreeNode tn=new TreeNode();

    tn.Text = dr["fullname"].ToString();

    tn.Value = dr["Id_empl"].ToString();

    nodes.Add(tn);

    tn.NavigateUrl = "javascript.void(0)";

    //If node has child nodes, then enable on-demand populating

    tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);

    }

    }

    #region protected void on_click(object sender,EveentArgs e)

    protected void on_click(object sender, EventArgs e)

    {

    PopulateRootLevel();

    }

    #endregion protected void on_click()

    }

    Thanks!!!!!!

  • I finally got an idea to make multi level treeview from database value or whatever you can represent as a Datatable. I guess you guys have no problem understanding a fully functioning c# code. so i am explaining less

    http://hippie-geek.spaces.live.com/blog/cns!EF72BA3F174204BE!1104.entry

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply