如何递归填充TreeView

时间:2014-02-26 08:00:00

标签: c# winforms recursion

好吧,我在一个名为 departments 的数据库中有一个表,它有以下字段: Id,Parent,Name,IsActive

使用此数据填充TreeView的最佳方法是什么?

3 个答案:

答案 0 :(得分:13)

您可以使用以下

1-将数据库中的数据检索到datatable或list,将其称为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);
         }
    }
}

并在加载事件中

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

希望这会对你有所帮助

问候

答案 1 :(得分:0)

imagelist包含9张图片。

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

答案 2 :(得分:-1)

这是一个代码段。现在您可以根据您的问题进行更改

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