.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Treeview in ASP.NET

Posted By:Athira Sreejith       Posted Date: March 24, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Sample code example to use treeview control in asp.net. this example in in C#.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class LoginPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                string projects = "select * from projects";
                string groups = "select * from groups";
                string tasks = "select * from tasks";
                string combinedString = projects + ";" + groups + ";" + tasks;            


                DataSet dsNew = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(combinedString, conn);
                da.Fill(dsNew);
                dsNew.Tables["Table"].TableName = "projects";
                dsNew.Tables["Table1"].TableName = "groups";
                dsNew.Tables["Table2"].TableName = "tasks";

                DataRelation rlProjectsGroups = new DataRelation("ProjToGrp", dsNew.Tables["projects"].Columns["pid"], dsNew.Tables["groups"].Columns["pid"]);
                dsNew.Relations.Add(rlProjectsGroups);

                DataRelation rlGroupsTasks = new DataRelation("GrpToTask", dsNew.Tables["groups"].Columns["gid"], dsNew.Tables["tasks"].Columns["gid"]);
                dsNew.Relations.Add(rlGroupsTasks);

                TreeNode Tprojects, Tgroups, Ttasks;
             
                foreach (DataRow projectRow in dsNew.Tables["projects"].Rows)
                {
                    Tprojects = new TreeNode();
                    Tprojects.Text = projectRow["pname"].ToString();
                    Tprojects.Value = projectRow["pid"].ToString();
                    TreeView1.Nodes.Add(Tprojects);

                    foreach (DataRow groupRow in projectRow.GetChildRows("ProjToGrp"))
                    {
                        Tgroups = new TreeNode();
                        Tgroups.Text = groupRow["gname"].ToString();
                        Tgroups.Value = groupRow["gid"].ToString();
                        Tprojects.ChildNodes.Add(Tgroups);

                        foreach (DataRow taskRow in groupRow.GetChildRows("GrpToTask"))
                        {
                            Ttasks = new TreeNode();
                            Ttasks.Text = taskRow["tname"].ToString();
                            Ttasks.Value = taskRow["tid"].ToString();
                            Tgroups.ChildNodes.Add(Ttasks);
                        }

                    }
                }


            }
            TreeView1.CollapseAll();

        }
    }
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend