Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I want to create a TreeView Control. It will Display the SQL Table Data.
I have one table Named as DealerDetails.
It contains DealerCode and Location.
What I want is for Location to be the parent Node and DealerCode to work as a ChildNode.
Same Locations DealerCode should appear under the Location Node.

I want to populate the TreeView using SQL Data.

Please help me.
Posted
Updated 7-Jan-12 5:43am
v3
Comments
Kschuler 6-Jan-12 16:49pm    
You haven't asked a question. What are you having trouble with?
[no name] 7-Jan-12 11:13am    
My first sentence is my Question.
how can I populate a tree view using SQL data.

Since your question is openended, providing a precise solution could be difficult.

If you need a wpf or winforms based solution, this could be one of the many approach:

1. Load all the data you want from the sql server into datasets

2. Make sure you have all the relations defined on them (Master / detail), this would alter help in the hierarchical display.
Refer to http://msdn.microsoft.com/en-us/library/ms171913.aspx[^] for accessing related records.

3. You need a tree view control which can display multiple columns and should be able to display different columns at each level.
You can use multi level hierarchical data templates in wpf for this purpose.

If you wish to use ASP.Net you can simply use multi level nested GridView
Refer to Walkthrough: Creating a Nested GridView Control [^]
 
Share this answer
 
Comments
[no name] 7-Jan-12 14:14pm    
i'm not using WPF. i'm using c# 2.0
and I want how to use a treeview control that will contain sql data?(two columns only) as i mention in my question.
both the links are not useful for me.
void fill_Tree2()
       {

           SqlConnection con = new SqlConnection();
           con.ConnectionString = Global.constr;
           con.Open();
           SqlCommand cmd2 = new SqlCommand("Select distinct station from Dealers order by station", con);
           SqlDataAdapter da = new SqlDataAdapter(cmd2);
           DataSet PrSet = new DataSet();
           da.Fill(PrSet, "Dealers");

           treeView1.Nodes.Clear();

           foreach (DataRow dr in PrSet.Tables[0].Rows)
           {

               TreeNode tnParent = new TreeNode();

               tnParent.Text = dr["Station"].ToString();

               tnParent.Tag = dr["Station"].ToString();

               //   tnParent.PopulateOnDemand = true;

               //   tnParent.ToolTip = "Click to get Child";

               //   tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;

               tnParent.Expand();

               //   tnParent.Selected = true;

              treeView1.Nodes.Add(tnParent);

               FillChild(tnParent, tnParent.Tag.ToString());

           }
       }
       public void FillChild(TreeNode parent, string ParentId)
       {
           SqlConnection con = new SqlConnection();
           con.ConnectionString = Global.constr;
           con.Open();
           SqlCommand cmd2 = new SqlCommand("Select * from Dealers where Station ='" + ParentId+"'", con);
           SqlDataAdapter da = new SqlDataAdapter(cmd2);
           DataSet PDataset = new DataSet();
           da.Fill(PDataset, "Dealers");
           DataSet ds = PDataset;

           parent.Nodes.Clear();

           foreach (DataRow dr in ds.Tables[0].Rows)
           {

               TreeNode child = new TreeNode();

               child.Text = dr["Code"].ToString().Trim();

               child.Tag = dr["Code"].ToString().Trim();

               if (child.Nodes.Count == 0)
               {

                   //child.PopulateOnDemand = true;

               }

               // child.ToolTip = "Click to get Child";

               // child.SelectAction =TreeNodeSelectAction.SelectExpand;

              // child.CollapseAll();

               parent.Nodes.Add(child);

           }

       }
 
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