ARTICLE

How to: Generate nodes of Tree view control

Posted by Amit M Articles | ASP.NET using VB.NET June 18, 2007
The article shows that how to generate nodes of tree view control dynamically.
 
Reader Level:

Below code shows, how to generate nodes of treeview control dynamically.

<asp:TreeView ID="tree" runat="server" ForeColor="black" RootNodeStyle-ImageUrl="~/xpMyComp.gif" LeafNodeStyle-ImageUrl="~/help_page.gif"> </asp:TreeView>

C# Code

public void GenrateTreeView()

{

    DataSet myDataSet = GetData();

    foreach (DataRow parentRow in myDataSet.Tables["department"].Rows)

    {

        TreeNode parentNode = new TreeNode((string)parentRow["dname"]);

        tree.Nodes.Add(parentNode);

        foreach (DataRow childRow in parentRow.GetChildRows("Child"))

        {

            TreeNode childNode = new TreeNode((string)childRow["ename"]);

            parentNode.ChildNodes.Add(childNode);

        }

    }

}

public DataSet GetData()

{

    string strcon = System.Configuration.ConfigurationManager.AppSettings.Get("constring");

    string empQuery = "SELECT * FROM employee ";

    string deptQuery = "SELECT * FROM department";

    SqlConnection con = new SqlConnection();

    SqlDataAdapter da = new SqlDataAdapter(empQuery, strcon);

    SqlDataAdapter da1 = new SqlDataAdapter(deptQuery, strcon);

    DataSet myDataSet = new DataSet();

    da.Fill(myDataSet, "employee");

    da1.Fill(myDataSet, "department");

    myDataSet.Relations.Add("Child", myDataSet.Tables["department"].Columns["deptno"],

    myDataSet.Tables["employee"].Columns["deptno"]);

    return myDataSet;

}

share this article :
post comment
 

Hi Amit, i saw ur post How to: Generate nodes of Tree view control so check this url and try to sovle my problem http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=34203

Posted by love sharma Sep 22, 2007
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor