Hello, I am working on project in which i want a treeview should have multiple child that ie
There is one parent and that parent should have multiple child and all this through coding using Primary key and Forieng key so any body who know ot plz send me the code

Hello
I hope it can help you a bit :)

TreeNode treeNode = new TreeNode();
        treeNode = treeView.Nodes.Add("Parent");

        //Child        
        for (int i = 0 ; i<10; i++)
        {
            TreeNode childNode = New TreeNode();
            childNode.Text = "Chile" + i.ToString;

            //picture
            //childNode.ImageKey = i;

            //Picture when selected
            //childNode.SelectedImageKey = "C" & i;
        
            treeNode.Nodes.Add(childNode);

            //Child's Child Node       
            for (int j = 1; j <5; j++)
            {
                TreeNode childChildNode =  New TreeNode();
                childChildNode.Text = "childChildNode" + j.ToSrting();
                childNode.Nodes.Add(childChildNode);
            }
        }
DataTable dtTreeView= mDBMonthYear.ReadDataset("Select Department.DepartmentId,DepartmentName,UnitId,UnitName from Department inner join Unit on Department.DepartmentId=Unit.DepartmentId").Tables[0];
          if (dtTreeView.Rows.Count > 0)
          {
              TreeNode rootNode = tvDepartments.Nodes.Add("All");
              rootNode.ImageIndex = 0;
              for (int i = 0; i < dtTreeView.Rows.Count; i++)
              {
                  TreeNode parentNode = rootNode.Nodes.Add(dtTreeView.Rows[i]["DepartmentName"].ToString(), dtTreeView.Rows[i]["DepartmentName"].ToString(),Convert.ToInt32(dtTreeView.Rows[i]["DepartmentID"].ToString()));
                  parentNode.ImageIndex = 1;

                  TreeNode childNode = parentNode.Nodes.Add(dtTreeView.Rows[i]["UnitName"].ToString(), dtTreeView.Rows[i]["UnitName"].ToString(), Convert.ToInt32(dtTreeView.Rows[i]["UnitID"].ToString()));
                  childNode.ImageIndex = 2;

              }
              
          }

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

This article has been dead for over six months. Start a new discussion instead.