Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
.
Could you please provide me a code sample showing how to load a treelistview from database​ with hardcoded root nodes on treelistview.

I have shared the link below of a sample preview of the tree I am in need of. Rootnodes are hardcoded and I want the child nodes to be populated from a database.

http://tinypic.com/view.php?pic=qnr212&s=5#.Upl6WsSnrh0

Please shed some light here.

Thanks
Rahul​
Posted
Comments
Rahul Krishnan R 2-Dec-13 0:44am    
Any one out there for a help. ???????????
Jörgen Andersson 2-Dec-13 16:08pm    
Why don't you just add the rootnodes to the database?

 
Share this answer
 
Comments
Rahul Krishnan R 2-Dec-13 3:04am    
Thanks for the reply. I have checked all these and didnt help.
Please note that I am using Devexpress Treelistview.
If you could tell me how to programatically populate treelistview using C# that would be of much help as I know how to populate it from the property window using Keyfieldname and Parentfieldname.
do you want how get the data from DB or hard-code the data to tree listview?
first load data as usual from sql-server, assume you know how to search a hierarchical data.
then get the root node data and added to the tree view items. last add each node children one by one.

C#
//This demo is show the pc file system as a tree

 public void LoadTree(TreeNode parentNode)
{
    string path = parentNode.Tag as string;
    foreach (string drv in System.IO.Directory.GetFiles(path))
    {
        TreeNode driverNode = new TreeNode(drv);
        driverNode.Tag = drv;
        driverNode.Nodes.Clear();
        parentNode.Nodes.Add(driverNode);

    }

    foreach (string drv in System.IO.Directory.GetDirectories(path))
    {
        TreeNode driverNode = new TreeNode(drv);
        driverNode.Tag = drv;
        driverNode.Nodes.Clear();
        parentNode.Nodes.Add(driverNode);

    }
 }


 private void button1_Click(object sender, EventArgs e)
 {
     foreach (System.IO.DriveInfo drv in System.IO.DriveInfo.GetDrives())
     {
         TreeNode driverNode = new TreeNode(drv.Name);
         driverNode.Tag = drv.Name;
         driverNode.Nodes.Clear();
         LoadTree(driverNode);
         this.treeView1.Nodes[0].Nodes.Add(driverNode);
     }
}
 
Share this answer
 
Hi,

I managed to sort it out.

Below are the links you can refer for the answer:

http://rahulkrishnanr.blogspot.com/2013/12/load-treeview-from-datasource-with.html[^]

Grandchildnodes not loading in treeview : Windows Forms[^]

http://www.c-sharpcorner.com/UploadFile/c5c6e2/populate-a-treeview-dynamically/[^]


A code Sample here :

C#
foreach (DataRow dr in ds.Tables["groupmasterdetails"].Rows)
            {                
                TreeNode tn = new TreeNode(dr["GroupDesc"].ToString());
                foreach (DataRow drChild in dr.GetChildRows("test"))
                {
                    TreeNode ctn=new TreeNode(drChild["GroupDesc"].ToString());
                    foreach (DataRow drgrandChild in drChild.GetChildRows("test1"))
                    {
                        TreeNode gctn = new TreeNode(drgrandChild["AccountDesc"].ToString());
                        ctn.Nodes.Add(gctn);
                    }
                    tn.Nodes.Add(ctn);
                }
                treeView1.Nodes[0].Nodes.Add(tn);            
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900