I all way have problem with show data in Tree View.
I want to show data from database into Tree View
Can help me this problem?
Thank Advance,.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Niradhip ChakrabortyRank #429 · 5.8K reputationPosted Apr 29, 2009, 10:05 PM EDT
check out:
1.http://www.codeproject.com/KB/aspnet/FillTreeView.aspx
2.http://www.codeproject.com/KB/aspnet/FillTreeView.aspx?display=PrintAll
meli19 reputationPosted Apr 29, 2009, 1:54 PM EDT
Hi,
I have did similar thing on one of my page, where I build the "tree view" from the database.
In my case, I have 2 tables (parent table nad child table) and read them in using SQLDataReader, and then loop through it to fill out the tree.
meli19 reputationPosted Apr 29, 2009, 2:02 PM EDT
BTW, here is the example of the code:
private
void fill_Tree(){
/** Fill the treeview control Root Nodes From Parent Table
* and child nodes from ChildTables
*/
SqlConnection SqlCon = new SqlConnection(connection);SqlCon.Open();
/** Query the database
*/
SqlCommand SqlCmd = new SqlCommand("Select * from PrentTable", SqlCon); /**Define and Populate the SQL DataReader
*/
SqlDataReader Sdr = SqlCmd.ExecuteReader(); /** Dispose the SQL Command to release resources
*/
SqlCmd.Dispose();
/** Initialize the string ParentNode.*/
string[,] ParentNode = new string[100, 2]; int count = 0; while (Sdr.Read()){
ParentNode[count, 0] = Sdr.GetValue(Sdr.GetOrdinal(
"PKinParentTbl")).ToString();ParentNode[count++, 1] = Sdr.GetValue(Sdr.GetOrdinal(
"ColumValue")).ToString();}
Sdr.Close();
for (int loop = 0; loop < count; loop++){
TreeNode root = new TreeNode();root.Text = ParentNode[loop, 1];
/** Give the url of your page
*/
root.NavigateUrl =
"~/TreeView.aspx"; SqlCommand Module_SqlCmd = new SqlCommand("Select * from ChildTable where ID =" + ParentNode[loop, 0] + "ORDER BY Description ASC", SqlCon); SqlDataReader Module_Sdr = Module_SqlCmd.ExecuteReader(); while (Module_Sdr.Read()){
TreeNode child = new TreeNode();child.Text = Module_Sdr.GetValue(Module_Sdr.GetOrdinal(
"Description")).ToString();child.Target =
"_blank"; //get the url string url = Module_Sdr.GetValue(Module_Sdr.GetOrdinal("ChildColValue")).ToString(); if (url != null)child.NavigateUrl = url;
root.ChildNodes.Add(child);}
Module_Sdr.Close();
// Add root node to TreeView
TreeView1.Nodes.Add(root);
}
/** By Default, when you populate TreeView Control programmatically, it expends all nodes.
*/
TreeView1.CollapseAll();
SqlCon.Close();
}