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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Niradhip ChakrabortyPosted Apr 30, 2009, 2:05 AM
check out:
1.http://www.codeproject.com/KB/aspnet/FillTreeView.aspx
2.http://www.codeproject.com/KB/aspnet/FillTreeView.aspx?display=PrintAll
meliPosted Apr 29, 2009, 6:02 PM
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();
}
meliPosted Apr 29, 2009, 5:54 PM
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.