Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am using a treeview to populate data from an sql table. I have Parent and Child Nodes in one table and Grand Child Nodes in another table.

Below is my code :

C#
string groupmaster = "select GroupID,GroupDesc,ParentGroupID from aGroupMaster";
            string accountmaster = "select * from aaccountmaster";
            SqlCommand groupmastercmd = new SqlCommand(groupmaster, DBConnection.Getconnection());
            SqlCommand accountmastercmd = new SqlCommand(accountmaster, DBConnection.Getconnection());
            groupmastercmd.CommandType = CommandType.Text;
            accountmastercmd.CommandType = CommandType.Text;
            SqlDataAdapter groupmasterda = new SqlDataAdapter(groupmastercmd);
            SqlDataAdapter accountmasterda = new SqlDataAdapter(accountmastercmd);
            DataSet ds = new DataSet();
            groupmasterda.Fill(ds, "groupmasterdetails");
            accountmasterda.Fill(ds, "accountmasterdetails");
            ds.Relations.Add("test", ds.Tables["groupmasterdetails"].Columns["GroupID"],
                ds.Tables["groupmasterdetails"].Columns["ParentGroupID"]);
            ds.Relations.Add("test1", ds.Tables["groupmasterdetails"].Columns["GroupID"],
                ds.Tables["accountmasterdetails"].Columns["GroupID"]);
            foreach (DataRow dr in ds.Tables["groupmasterdetails"].Rows)
            {                
                TreeNode tn = new TreeNode(dr["GroupDesc"].ToString());
                foreach (DataRow drChild in dr.GetChildRows("test"))
                {                     
                    tn.Nodes.Add(drChild["GroupDesc"].ToString());
                }
                treeView1.Nodes[0].Nodes.Add(tn);            
            }


The below link will take you to an example image of my treeview.

http://imgur.com/wY8vDoZ

In this example, I have marked the parent nodes.

I have 2 problems to be solved here :

1. I could get the parent nodes and child nodes to the treeview but cannot get the grandchildnodes in it.

2. The child nodes are again appearing as parent node. Example is marked in green box.

Please advise me some solutions ASAP.

Thanks
Rahul
Posted
Updated 2-Dec-13 20:45pm
v2
Comments
Rahul Krishnan R 3-Dec-13 3:59am    
I got the first problem solved : Trying to resolve second problem. Any help is much appreciated. I posted the solution and delete because the thread got marked as solved. But 2nd problem still exist and I am looking for a solution for that problem now.

:)

Thanks
johannesnestler 3-Dec-13 5:01am    
Add the nodes to the correct parent notes - what else should be the problem? The code you are showing does just two "Levels" of nodes - childs(outer foreach) and granchilds (inner foreach) if I Interpret that correct - so it would help if you show the relevant/real code...
Rahul Krishnan R 3-Dec-13 5:52am    
Hi johannesnestler,

In the code you can see both parent and child data columns are same "GroupDesc". So each time recursion occurs, each row in the data table in added as parent. That is the problem I guess. Need to sort this out.

Thanks
Rahul Krishnan R 7-Dec-13 23:11pm    
Any updates from any one on the second problem here ?

Hi Guys,

Got it solved.
Courtesy to the link below.

http://www.c-sharpcorner.com/UploadFile/c5c6e2/populate-a-treeview-dynamically/
 
Share this answer
 
Comments
Rahul Krishnan R 8-Dec-13 7:04am    
Find the full code in my blog:

http://rahulkrishnanr.blogspot.com/2013/12/load-treeview-from-datasource-with.html
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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