ASP.NET Problem with TreeView bind from datasource

  • Thread starter Thread starter ai1231
  • Start date Start date
A

ai1231

Guest
Hi

I am trying to populate TreeView from database but I am having problems with sub child nodes

Why "Durres" doesnt show up as child of "Tirana" and why "Shkoder" doesnt show up as child of "Durres"?

Database code

CREATE TABLE [dbo].[TopBill] (
[srNo] INT NOT NULL IDENTITY,
[Pid] INT NULL,
[PName] VARCHAR (50) NULL,
[PDetails] NCHAR (10) DEFAULT (NULL) NULL,
[cId] INT NULL,
[Cname] VARCHAR (50) NULL,
[Cqty] INT DEFAULT (NULL) NULL,
CONSTRAINT [PK_TopBill] PRIMARY KEY CLUSTERED ([srNo] ASC)
);


Database data

1513714.png

C# code

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class Index : System.Web.UI.Page
{
Boolean m_bNodeFound;
Boolean intNodeFound;
string sValuepath;
protected void Page_Load(object sender, EventArgs e)
{




if (!IsPostBack)
{

using (var connection = new SqlConnection("Data Source=(localdb)\\ProjectsV13;Initial Catalog=gab;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"))
{
string com = "Select * from dbo.TopBill";
SqlDataAdapter adpt = new SqlDataAdapter(com, connection);
DataTable dt = new DataTable();
adpt.Fill(dt);
connection.Open();
TreeNode root = new TreeNode();

root = new TreeNode();
root.Text = dt.Rows[0][5].ToString();
root.Value = dt.Rows[0][5].ToString();
TreeView1.Nodes.Add(root);
for (int i = 0; i < dt.Rows.Count; i++)
{
//make NodeNotFound at each row
m_bNodeFound = false;

//root as it is
TreeNode tn = root;

//get NodeName by cName
string nodeName = dt.Rows[5].ToString();

//Check if it is not null, to skip errors
if (nodeName != null && nodeName != "")
{
//MainNodecollections
TreeNodeCollection nodes = TreeView1.Nodes;

//Check if node already exists in Treeview
FindNodeInHierarchy(nodeName);

//If not found then continue
if (!m_bNodeFound)
{
//If node is root node
if (dt.Rows[2].ToString() == "" || dt.Rows[2].ToString() == null)
{
TreeNode root2 = new TreeNode();
root2.Text = dt.Rows[5].ToString();
root2.Value = dt.Rows[5].ToString();
TreeView1.Nodes.Add(root2);
}
//Check if node is child node
else if (CheckRowsAllValues(dt.Rows[1].ToString(), dt))
{
//Find New Root of child node
TreeNode NewRoot = FindRoot(dt.Rows[1].ToString(), dt, root);
//if New root is not empty
if (NewRoot != null)
{
TreeNode root2 = new TreeNode();
root2.Text = dt.Rows[5].ToString();
root2.Value = dt.Rows[5].ToString();
//append child node(current value) with new root
NewRoot.ChildNodes.Add(root2);
}

}
}

}
}

}
}
}

private TreeNode FindRoot(string v, DataTable dt, TreeNode tNode)
{
string expression = "cId = " + v;
DataRow[] foundRows;
foundRows = dt.Select(expression);

//Find node using Id of table row
TreeNode tn = TreeView1.FindNode(foundRows[0][0].ToString());

//if not found, search using Name
if (tn == null && foundRows[0][5].ToString() != "")
{
var value = foundRows[0][5].ToString();
TreeNode searchedNode = null;

//search node by Value(City Name) by looping each node
foreach (TreeNode node in TreeView1.Nodes)
{
if (searchedNode == null)
{
searchedNode = SearchNode(node, value);
if (searchedNode == null)
{
foreach (TreeNode childNode in node.ChildNodes)
{
searchedNode = SearchNode(childNode, value);
if (searchedNode != null)
tn = searchedNode;
}
}
else
{
break;
}
}
else
{
break;
}
}
tn = searchedNode;

}

return tn;
}

//Search Node code
private TreeNode SearchNode(TreeNode node, string searchText = null)
{
if (node.Text == searchText) return node;

TreeNode tn = null;
foreach (TreeNode childNode in node.ChildNodes)
{
tn = SearchNode(childNode);
if (tn != null) break;
}

if (tn != null) node.Expand();
return tn;
}

private bool CheckRowsAllValues(string v, DataTable dt)
{
string expression = "cId = " + v;
DataRow[] foundRows;
foundRows = dt.Select(expression);
if (foundRows.Count() > 0)
{
return true;
}
else
{
return false;
}
}



private void FindNodeByValue(string strValue)
{
// If the TreeView control contains any root nodes, perform a
// preorder traversal of the tree and display the text of each node.
if (TreeView1.Nodes.Count > 0)
{

// Iterate through the root nodes in the Nodes property.
for (int i = 0; i < TreeView1.Nodes.Count; i++)
{

// Display the nodes.
DisplayChildNodeText(TreeView1.Nodes, strValue);

}

}
}


void DisplayChildNodeText(TreeNode node, string strValue)
{

// Display the node's text value.
//Message.Text += node.Text + "<br />";
if (strValue == node.Text.ToString())
{
sValuepath = node.ValuePath;
intNodeFound = true;
}

// Iterate through the child nodes of the parent node passed into
// this method and display their values.
for (int i = 0; i < node.ChildNodes.Count; i++)
{

DisplayChildNodeText(node.ChildNodes, strValue);

}

if (intNodeFound) return;

}



private void FindNodeInHierarchy(string strSearchValue)
{

// If the TreeView control contains any root nodes, perform a
// preorder traversal of the tree and display the text of each node.
if (TreeView1.Nodes.Count > 0)
{

// Iterate through the root nodes in the Nodes property.
for (int i = 0; i < TreeView1.Nodes.Count; i++)
{

// Display the nodes.
CheckChildNodeText(TreeView1.Nodes, strSearchValue);

}

}
}


void CheckChildNodeText(TreeNode node, string strValue)
{

// Display the node's text value.
//Message.Text += node.Text + "<br />";
if (strValue == node.Text.ToString())
{
m_bNodeFound = true;
}

// Iterate through the child nodes of the parent node passed into
// this method and display their values.
for (int i = 0; i < node.ChildNodes.Count; i++)
{

DisplayChildNodeText(node.ChildNodes, strValue);

}

if (m_bNodeFound) return;

}
}
}



The result

1513715.png

What code should do or what I want in this case

1513716.png

Continue reading...
 
Back
Top