6
Answers

How to create N Level Of TreeView from database in asp.net

Hello Guys !!
 
I have already asked the same question but now  i am again asking with new requirment, so please read carefully and solve my issues.
 
 My Requirment:
  •    How to create n level of Treeview in asp.net c# from SQL Database Record.
Explaining my Requirment: -  i have a table and there is Srno,PId, Pname, CID, CName,Cqty colums in my table.
 
 
Suppose if CID is also in PID column,  Node must be created on that CID and also must be expand button to show the child of that cID.
 
What i have done in coding let's see...
 
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="N-level-Treeview.aspx.cs"
Inherits="N_level_Treeview" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer" NodeIndent="15">
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
</asp:TreeView>
</div>
</form>
</body>
</html>
 
CS-------- 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class N_level_Treeview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = this.GetData("SELECT cId, cName FROM TopBill");
this.PopulateTreeView(dt, 0, null);
}
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["cName"].ToString(),
Value = row["cId"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("SELECT srNo,cid, cName FROM TopBill WHERE pid = " + child.Value);
PopulateTreeView(dtChild, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
}
 
-------------- Current Output  of my Code is ....
 
 
 Required Result format is like---------
 
 
In above picture , I am trying to show that if India has some child node, India Must me expandable so that i can see delhi,  If again Delhi Has some child Like Preet vihar , Laxmi Nagar, Delhi also must be expandable to see their child  and so on....
 
 
 
Please solve this issues..
 
 
 
 
 

Attachment: n-level-tree-view.rar

Answers (6)

2
Photo of Nitin Sontakke
NA 11.7k 2.2k 7y
To begin with in your Page_Load event, add the following WHERE clause:
 
WHERE [Pid] = 0 
Accepted
2
Photo of Nitin Sontakke
NA 11.7k 2.2k 7y
Here is a modified and perfectly working code...
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.   if (!this.IsPostBack)  
  4.   {  
  5.     DataTable dt = this.GetData("SELECT cId, cName FROM TopBill where [Pid] = 0");  
  6.     this.PopulateTreeView(dt, null);  
  7.   }  
  8. }  
  9.   
  10. private void PopulateTreeView(DataTable dtParent, TreeNode treeNode)  
  11. {  
  12.   foreach (DataRow row in dtParent.Rows)  
  13.   {  
  14.     TreeNode child = new TreeNode  
  15.     {  
  16.       Text = row["cName"].ToString(),  
  17.       Value = row["cId"].ToString()  
  18.     };  
  19.     if (treeNode == null)  
  20.       TreeView1.Nodes.Add(child);  
  21.     else  
  22.       treeNode.ChildNodes.Add(child);  
  23.     DataTable dtChild = this.GetData("SELECT srNo,cid, cName FROM TopBill WHERE pid = " + child.Value);  
  24.     if (dtChild.Rows.Count != 0)  
  25.       PopulateTreeView(dtChild, child);  
  26.   }  
  27. }  
Bihar might appear at wrong position, but that is because of your data and not code.
 
0
Photo of Nitin Sontakke
NA 11.7k 2.2k 7y
Where is the code?
 
0
Photo of Vivek Kumar Vishwas
NA 117 5.1k 7y
Sir, its reallry working fine Thank You so much.
Now what my requirment it to add or calculate the value from treeview nodes.
See the below picture..
 
Now suppose sir, i want to show CQty   value with each cid and i also wanto calculate total value of cqty.  How to do this.
 
Let me clear once again.
 
Supoose that there  is a one product(Pen- and its cid is 10), pen contains with it self like Cap, Refil, Ink, color, and so on.
 
the the properties of pen contains there cqty Value, so how do we find that what cqty is required to make 1 Pen.
 
i hope i cleared my  requirments.
Help me sir... 
 
 
0
Photo of Vivek Kumar Vishwas
NA 117 5.1k 7y
 Sir, Its working good till this level and showing result like below.
 
 Now suppose that  i also added some value under Preet vihar as showing in below picture.
 
 
 
 Now how do we make preet vihar as a parent for its both child node as you can see in the above picture and its not the last stage of tree view it can of n level of hierarchy.
 
 please check and help me out from this problem.
 
 
 
 
 
 
0
Photo of Vivek Kumar Vishwas
NA 117 5.1k 7y
it will only bind only parent Node not its child node. I have tried it many times.May you please download my files and check the process of creating Treeview nodes from Parent to its child at n level hierarchy.