Asp.net Treeview control - Adding parent with n- number of child

Cp_raj
Posted by in ASP.NET category on for Beginner level | Views : 19495 red flag
Rating: 5 out of 5  
 1 vote(s)

To Add n- number of child to the treeview


 Download source code for Asp.net Treeview control - Adding parent with n- number of child

Introduction

To Add n-number of child to the parent in the Treeview control.

Database structure

NODE_PK

PARENT_PK

NODE_NAME

NODE_DESC

-1

0

India

Country in Asia

1

-1

North India

North part

2

-1

South India

Southern part

3

1

Utter pradesh

State

4

1

Punjab

 

5

1

Assam

 

6

1

W.Bengal

 

7

2

TamilNadu

 

8

7

Chennai

 

9

7

Coimbatore

 

Node_Pk - Node ID of the parent

Parent_PK - Parent ID for the Node

Node_ Name - Name of the Node

Node_Desc- Just the description of the Node Name

In this example India will be the parent and  NorthIndia and SouthIndia will be the child Node for India.

Below NorthIndia you can bring all the child nodes like State,City,Town,Village..etc(n-Number of child).

Its just a example change according to your project

Code


The Parent Node of the parent data can start with 0 or Null.So First add the parent Node, to the treeview.

And  check the dataset whether the parent has any child, if it has child add  the child node to particular parent node. Search the dataset using dt.select().It will return the datarow[].Fill the child node by looping the datarow.

----To add parent-----Use name instead of Index value--

TreeNode root = new TreeNode();
root.Text = dt.Rows[0][2].ToString();
root.Value = dt.Rows[0][0].ToString();
TreeView1.Nodes.Add(root);


for (int i = 0; i < dt.Rows.Count; i++)
{
string nodeName = dt.Rows[i][2].ToString();
TreeNodeCollection nodes = TreeView1.Nodes;
FindNodeInHierarchy(nodeName);

if (!m_bNodeFound)
{
TreeNode root2 = new TreeNode();
root2.Text = dt.Rows[i][2].ToString();
root2.Value = dt.Rows[i][0].ToString();
TreeView1.Nodes.Add(root2);
}

else
{
string expression = "PARENT_PK = " + dt.Rows[i][0].ToString();
DataRow[] foundRows;
foundRows = dt.Select(expression);
//FindNodeByValue(nodes, dt.Rows[i][0].ToString());
FindNodeByValue(nodeName);
if (intNodeFound == true)
{
tn = TreeView1.FindNode(Server.HtmlEncode(sValuepath.Trim()));
}
for (int j = 0; j < foundRows.Length; j++)
{
TreeNode root1 = new TreeNode();
root1.Text = foundRows[j][2].ToString();
root1.Value = foundRows[j][0].ToString();
tn.ChildNodes.Add(root1);
}
}

}

Please feel free to download the attachment for complete code snippet and clear understanding.

Thanks


Page copy protected against web site content infringement by Copyscape

About the Author

Cp_raj
Full Name: Rajkumar Pauldurai
Member Level: Starter
Member Status: Member
Member Since: 5/26/2009 6:29:52 AM
Country:

http://www.dotnetfunda.com
I started my career with Asp,ms-access,sql server 7,xml.Then i updated to .net,share point server,Basics about Biztalk server

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)