Visual Studio .NET - Binding Dataset to Treeview control in asp.net

Asked By Shreya Trivedi
31-Aug-08 08:59 AM
How can I bind Treeview to a dataset in asp.net ?

Reply  Reply

31-Aug-08 09:59 AM

See this example:

DataRow row = dataSet.Tables[0].NewRow();
row["ID"] = 1;
row["Text"] = "Parent 1";
dataSet.Tables[0].Rows.Add(row);

row = dataSet.Tables[0].NewRow();
row["ID"] = 2;
row["Text"] = "Parent 2";
dataSet.Tables[0].Rows.Add(row);

row = dataSet.Tables[0].NewRow();
row["ID"] = 3;
row["ParentID"] = 1;
row["Text"] = "Child 1";
dataSet.Tables[0].Rows.Add(row);

row = dataSet.Tables[0].NewRow();
row["ID"] = 4;
row["ParentID"] = 1;
row["Text"] = "Child 2";
dataSet.Tables[0].Rows.Add(row);

row = dataSet.Tables[0].NewRow();
row["ID"] = 5;
row["ParentID"] = 2;
row["Text"] = "Child 3";
dataSet.Tables[0].Rows.Add(row);

row = dataSet.Tables[0].NewRow();
row["ID"] = 6;
row["ParentID"] = 2;
row["Text"] = "Child 4";
dataSet.Tables[0].Rows.Add(row);

TreeView1.DataSource = new HierarchicalDataSet(dataSet, "ID", "ParentID");

Go through this link:

http://www.codeproject.com/KB/aspnet/aspnettreeviewbind.aspx

 

links  links

31-Aug-08 10:49 AM

I am able to see couple of links which can help you out!

Binding ASP.NET TreeView to a DataSet or an ObjectDataSource

http://www.codeproject.com/KB/aspnet/aspnettreeviewbind.aspx

How to populate TreeView control using DataAdapter and DataRelation Objects

http://www.devasp.net/net/articles/display/289.html

Google for it, you will get lot of inputs!

reply  reply

31-Aug-08 10:51 PM

See this example also. Hope u can get a help:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

    Const connectionString As String = _
      "Server=localhost;Integrated Security=True;Database=Northwind"
    
    Sub Page_Load()
        if NOT IsPostBack Then
            PopulateNodes()
        End If
    End Sub

    Sub PopulateNodes()
        Dim dst As DataSet = GetTreeViewData()
        for each masterRow As DataRow in dst.Tables("Categories").Rows
            Dim masterNode As New TreeNode(masterRow("CategoryName").ToString())
            masterNode.NavigateUrl = ("Default2.aspx?src=" & masterRow("CategoryName").ToString())
            TreeView1.Nodes.Add(masterNode)
            for each childRow As DataRow in _
              masterRow.GetChildRows("Children")
                Dim childNode As New TreeNode(childRow("ProductName").ToString())
                childNode.NavigateUrl = ("Default2.aspx?src=" & childRow("ProductName").ToString())
                masterNode.ChildNodes.Add(childNode)
            next
        Next
    End Sub   
        
    Function GetTreeViewData() As DataSet
        Dim con As SqlConnection = new SqlConnection(connectionString)
        Dim dadCats As SqlDataAdapter = new _
          SqlDataAdapter("SELECT * FROM Categories", con)
        Dim dadProducts As SqlDataAdapter = new _
          SqlDataAdapter("SELECT * FROM Products", con)
        Dim dst As DataSet = new DataSet()
        dadCats.Fill(dst, "Categories")
        dadProducts.Fill(dst, "Products")
        dst.Relations.Add("Children", _
            dst.Tables("Categories").Columns("CategoryID"), dst.Tables("Products").Columns("CategoryID"))
        return dst
    End Function
    
</script>

<html>
<head>
    <title>Master/Detail TreeView</title>
</head>
<body>
    <form id="form1" runat="server">
    
    <asp:TreeView
        ID="TreeView1"
        Runat="Server"
        ShowExpandCollapse=true
        ImageSet=Faq
         ExpandDepth=0
        />
       To modify the look of the control try editing the "ImageSet" property of the treeview control...<br><br>
You can also use the imageUrl attribute of the node to provide custom images for the treeview..
 
    </form>
</body>
</html>
http://www.tek-tips.com/faqs.cfm?fid=6177
Create New Account
help
Wise for Visual Studio.NET Wise for Visual Studio.NET By Peter A. Bromberg, Ph.D. To "Print This Page" Link Peter Bromberg Wise for Visual Studio .NET is a total and complete installation development system for creating and editing Windows® Installer
visual studio.net 2003 and Access 2007 database .NET Framework Hi I am currently using Visual Studio.Net 2003 running on Windows Server 2000 operating system. I have used Visual Studio.net 2003 connecting to Access 2002 databases in the pass with great success. Now
Visual Studio.NET: Help Yourself! Visual Studio.NET: Help Yourself! By Peter A. Bromberg, Ph.D. Peter Bromberg I'm constantly annoyed by the number of developers using Visual Studio.NET who don't seem to see the need to create decent documentation for their
Migrating to Visual Studio.NET 2003: A Developer Perspective Migrating to Visual Studio.Net 2003: A Developer - oriented analysis By Peter A. Bromberg, Ph.D. Printer - Friendly Version Peter Bromberg A new Platform On April 24, 2003 Microsoft Windows Server 2003 and Visual Studio .NET 2003 are officially released. Many developers and beta testers (myself included) have been feverishly
How to take an IE rendered screenshot of a website with visual studio .net 2002 or visual stuido .net 2003? I can't install visual studio .net 2005 on this computer. .NET Framework How to take an IE rendered screenshot of a