AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 How to bind 3 Level TreeView control with Database?
Posted by Vivek kulshrestha on  3/4/2008 6:30:11 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
Sir i want to make 3 lavel tree view from 3 differwnt data base table(SQL server 2000)
Like this
Project
SubProj-1
File1
File2
SubProj-2
File3
File4
File5
Project-2
SubProj-1
File1
File2
SubProj-2
File3
File4
File5
Plz sen me code URGENT

Post Your Reply Post New Question

 Viewer's Reply
Dynamic Binding of TreeView control from Database Tables
Answered By Moderator1 on 6/18/2010 2:56:04 AM
Hi,

Follow the Steps Below,

Step 1: Add TreeView control to your aspx page and change the format and then Add a parent Note. The Html code will look like below,

<asp:TreeView ID="TreeView1" runat="server" ImageSet="Simple"
ontreenodepopulate="TreeView1_TreeNodePopulate" ExpandDepth="0">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD"
HorizontalPadding="0px" VerticalPadding="0px" />
<Nodes>
<asp:TreeNode Expanded="True" Text="Inventory" Value="Inventory" PopulateOnDemand="true"></asp:TreeNode>
</Nodes>
<NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black"
HorizontalPadding="0px" NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>


Step 2: Write 2 Methods to retrieve the Category and the Product in every category. Both method should get the TreeNode parameter as follows,

private void GetCategories(TreeNode node)
{
string sql = "Select * from category";
SqlDataAdapter da = new SqlDataAdapter(sql, this.TestConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
TreeNode newnode = new TreeNode(dr["CategoryDesc"].ToString(), dr["CategoryID"].ToString());
newnode.SelectAction = TreeNodeSelectAction.Expand;
newnode.PopulateOnDemand = true;
node.ChildNodes.Add(newnode);
}
}

private void GetProducts(TreeNode node)
{
string sql = "Select * from Products Where CategoryId=@CategoryId";
SqlDataAdapter da = new SqlDataAdapter(sql, this.TestConnectionString);
da.SelectCommand.Parameters.Add("@CategoryId", SqlDbType.Int).Value = node.Value;
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
TreeNode newnode = new TreeNode(dr["ProductName"].ToString(), dr["ProductID"].ToString());
node.ChildNodes.Add(newnode);
}
}

Step 3: Write code on the TreeNodePopulate event to bind the TreeView control with Cateogry as 1st level node and Products as second level nodes as follows,

protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
switch (e.Node.Depth)
{
case 0:
GetCategories(e.Node);
break;
case 1:
GetProducts(e.Node);
break;
}
}

Now, browse this page. You can see TreeView control populated with nodes. You can add any number of nodes by slightly modifying the above code.
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Integrating Silverlight 4 with SharePoint 2010 - Free 42 Page Sample Chapter
The Beginner's Guide to Joomla
The Art & Science of JavaScript - Free 100 Page Preview!