Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Grandchild nodes not loading in treeview :Windows Forms, Child nodes are appearing as parent nodes.

$
0
0

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:

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 Image is an example of my treeview. 
 

 
 
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


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>