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

Top 5 Contributors of the Month

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);
                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"]);

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

                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();

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

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





Further Readings:


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