How to populate TreeView recursively

Well, I have a table in the database called departments, and it has the following fields: Id, Parent, Name, IsActive.

What is the best way to populate a TreeView with this data?

+6
source share
3 answers

you can use the following

1- retrieve data from the database in a datatable or list call it as dataList

public void PopulateTree(ref TreeNode root,List<Department> departments)
{
    if(root==null)
    {
          root=new TreeNode();
          root.Text="Departments";
          root.Tag=null;
          // get all departments in the list with parent is null
          var details=departments.Where(t=>t.Parent==null);
          foreach(var detail in details)
          {
              var child= new TreeNode(){
                  Text=detail.Name,
                  Tage=detail.Id,
              };
              PopulateTree(ref child,departments);
              root.Nodes.Add(child);
          }      
    }
    else
    {
         var id=(int)root.Tag;
         var details=departments.Where(t=>t.Parent==id);
         foreach(var detail in details)
         {
              var child= new TreeNode(){
                  Text=detail.Name,
                  Tage=detail.Id,
              };
              PopulateTree(ref child,departments);
              root.Nodes.Add(child);
         }
    }
}

and in the Load event

TreeNode root=null;
var departments=query from database
PopulateTree(ref root,departments);

hope this helps you

considers

+15
source

imagelist contains 9 images.

    private void grupAltGrup_Load(object sender, EventArgs e)
    {   DataTable dtt = veritabani.tablogonder("select id, ad from grup where parent=-1");

        TreeNode parent = new TreeNode();
        parent.Text = "Kök";
        parent.Tag = "-1";
        parent.ImageIndex = 0;

        treeView1.Nodes.Add(parent);


        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            //       treeView1.Nodes.Add("Suppliers");
            TreeNode parent2 = new TreeNode();
            parent2.Text = dtt.Rows[i]["ad"].ToString();
            parent2.Tag = dtt.Rows[i]["id"].ToString(); 
            parent2.ImageIndex = 0;

            parent.Nodes.Add(parent2);
            treeviewDoldur(ref parent2,Convert.ToInt16(dtt.Rows[i]["id"]));
            //   child.Nodes.Add("Name: " + dtt.Rows[i]["id"].ToString());

        }

 int kacinciyavru = 1;
    void treeviewDoldur(ref TreeNode parent,int parentIdsi)
    {            DataTable yedek;
        yedek = veritabani.tablogonder("select id,ad from grup where parent=" + parentIdsi);
        for (int y = 0; y < yedek.Rows.Count; y++)
        {
            TreeNode child = new TreeNode();

            child.Text = yedek.Rows[y]["ad"].ToString();
            child.Tag = yedek.Rows[y]["id"].ToString();
            child.ImageIndex = kacinciyavru++;

           parent.Nodes.Add(child); 
            treeviewDoldur(ref child, Convert.ToInt16(yedek.Rows[y]["id"])); 

        }
        kacinciyavru--;
    }
0
source

Here is a snippet of code. Now you can change according to your problem

String strConn = "Server = .\\SQLEXPRESS;Database = Northwind;Integrated Security = SSPI;";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter("Select * from Products", conn);
SqlDataAdapter daCategories = new SqlDataAdapter("Select * from Categories", conn);
da.Fill(ds, "Products");
daCategories.Fill(ds, "Categories");
ds.Relations.Add("Cat_Product", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]);
foreach(DataRow dr in ds.Tables["Categories"].Rows)
{
       TreeNode tn = new TreeNode(dr["CategoryName"].ToString());
       foreach (DataRow drChild in dr.GetChildRows("Cat_Product"))
       {
              tn.Nodes.Add(drChild["ProductName"].ToString());
       }
       treeView1.Nodes.Add(tn);
}
-1
source

All Articles