Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am in a deep fix.
I have two table
ParentTable
Data

ParentID ParentName
   1         John
   2         Chris

Another Table
ChildTable
Data
ChildID ParentID ChildName
    1          1          aaa
    2          1          bbb
    3          2          ccc
    4          2          ddd

Now I want to show it in dynamic tree view
this is my dynamic tree.
TreeView myTreeView = new TreeView();
myTreeView.Location = new Point(100, 100);
myTreeView.Size = new Size(200, 200);
this.Controls.Add(myTreeView);

void fill_Tree2()
       {

           DataSet PrSet = "Select * from ParentTable";

           myTreeView.Nodes.Clear();

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

               TreeNode tnParent = new TreeNode();

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

               tnParent.Value = dr["ParentID"].ToString();

               //   tnParent.PopulateOnDemand = true;

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

               //   tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;

               tnParent.Expand();

               //   tnParent.Selected = true;

               myTreeView.Nodes.Add(tnParent);

               FillChild(tnParent, tnParent.Value);

           }



       }
       public void FillChild(TreeNode parent, string ParentId)
       {

           DataSet ds = PDataset("Select * from ChildTable where ParentId =" + ParentId);

           parent.ChildNodes.Clear();

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

               TreeNode child = new TreeNode();

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

               child.Value = dr["ChildID"].ToString().Trim();

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

                   child.PopulateOnDemand = true;

               }

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

              // child.SelectAction =TreeNodeSelectAction.SelectExpand;

               child.CollapseAll();

               parent.ChildNodes.Add(child);

           }

       }

It is desktop based application.I need parent and child id load but it is not support node.value
I am a novice developer,
Please help me.
Masud
Posted
Updated 15-Jun-10 0:45am
v3

your fill child should modified as
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
 
What you can do is:
C#
void fill_Tree2()
        {

            DataSet PrSet = "Select * from ParentTable";

            myTreeView.Nodes.Clear();

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

                TreeNode tnParent = new TreeNode();

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

                tnParent.Tag = dr["ParentID"].ToString();  // <====== change here ============

                //   tnParent.PopulateOnDemand = true;

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

                //   tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;

                tnParent.Expand();

                //   tnParent.Selected = true;

                myTreeView.Nodes.Add(tnParent);

                FillChild(tnParent, (string)tnParent.Tag); // <=== change here =====

            }


and make the same change in your FillChild method.
 
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