user3354807
user3354807

Reputation: 89

How to populate a TreeView recursively

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

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

Upvotes: 8

Views: 15683

Answers (3)

Fatih Topcu
Fatih Topcu

Reputation: 132

imagelist containes 9 pictures.

    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--;
    }

Upvotes: 0

Monah
Monah

Reputation: 6794

you can use the following

1- retrieve the data from the database into 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 that this will help you

regards

Upvotes: 20

jiten
jiten

Reputation: 5264

Here is a code snippet.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);
}

Upvotes: 0

Related Questions