绑定递归treeView

时间:2013-05-21 02:17:11

标签: c# ado.net linq-to-entities treeview

我正在开发Visual Sudio上的Windows窗体应用程序,这是我第一次使用treeView。我的项目有一个ADO .NET实体数据模型和SQL数据库,我有3个关系表:

  • CategoryidCategory, nameCategory
  • subCategoryidSubCategory, nameSubCategorie, idCategory
  • ArticlesidArticle, nameArticle, idSubCategory

我正在尝试使用此数据库中的递归创建一个treeView,最终得到如下结果,我被困在这里有人可以帮我吗? 这是我想要展示的内容:

+Category1
   +subCategory1-1
      article1
      article2
      article3
   +subCategory1-2
      article1
      article2
      article3
   +subCategory1-3
      article1
      article2
      article3
+Category2
   +subCategory2-1
      article1
      article2
      article3
   +subCategory2-2
      article1
      article2
      article3
   +subCategory2-3
      article1
      article2
      article3

用于绑定递归treeView的模板代码来自数据库usinf LINQ或教程链接,显示如何做到这将非常有帮助!感谢。

1 个答案:

答案 0 :(得分:0)

我认为这可能会对您有所帮助,这里我只在我的代码中填充三个表,您也可以直接从数据库中获取...

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }
    public void Bind()
    {
        DataTable dt = Get_Category();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            TreeNode node = new TreeNode();
            node.Text = dt.Rows[i]["idCategory"].ToString();
            node.Value = dt.Rows[i]["nameCategory"].ToString();
            AddSubCategory(node);
            this.TreeView1.Nodes.Add(node);
        }
    }
    public void AddSubCategory(TreeNode node)
    {
        DataRow[] datarows = Get_SubCategory()
                             .Select("idCategory='" + node.Value + "'");
        foreach (DataRow dr in datarows)
        {
            TreeNode childnode = new TreeNode();
            childnode.Text = dr["nameSubCategory"].ToString();
            childnode.Value = dr["idSubCategory"].ToString();
            AddArticles(childnode);
            node.ChildNodes.Add(childnode);
        }
    }
    public void AddArticles(TreeNode node)
    {
        DataRow[] datarows = Get_Articles()
                            .Select("idSubCategory='" + no.Value + "'");
        foreach (DataRow dr in datarows)
        {
            TreeNode childnode = new TreeNode();
            childnode.Text = dr["Subject"].ToString();
            childnode.Value = dr["SubjectID"].ToString();
            node.ChildNodes.Add(childnode);
        }
    }
    public DataTable Get_Category()
    {
        DataTable dt = new DataTable();
        DataRow dr;
        string IDs = "1,2,3,4";
        string categories = "Category1,Category2,Category3,Category4";
        string[] list1 = IDs.Split(',');
        string[] list2 = categories.Split(',');
        dt.Columns.Add(new DataColumn("nameCategory"));
        dt.Columns.Add(new DataColumn("idCategory"));
        for (int i = 0; i < list1.Length; i++)
        {
            dr = dt.NewRow();
            dr["idCategory"] = list1[i];
            dr["nameCategory"] = list2[i];
            dt.Rows.Add(dr);
        }
        return dt;
    }
    public DataTable Get_SubCategory()
    {
        DataTable dt = new DataTable();
        DataRow dr;
        string IDs = "1,2,3,4,5,6,7,8,9";
        string subCategories = "SubCategory1, SubCategory2, SubCategory3, 
                                SubCategory4, SubCategory5, SubCategory6, 
                                SubCategory7, SubCategory8, SubCategory9";
        string idCategory = "1,2,3,4,1,2,3,4,1";
        string[] list1 = IDs.Split(',');
        string[] list2 = subCategories.Split(',');
        string[] list3 = idCategory.Split(',');
        dt.Columns.Add(new DataColumn("nameSubCategory"));
        dt.Columns.Add(new DataColumn("idSubCategory"));
        dt.Columns.Add(new DataColumn("idCategory"));
        for (int i = 0; i < list1.Length; i++)
        {
            dr = dt.NewRow();
            dr["idSubCategory"] = list1[i];
            dr["nameSubCategory"] = list2[i];
            dr["idCategory"] = list3[i];
            dt.Rows.Add(dr);
        }
        return dt;
    }
    public DataTable Get_Articles()
    {
        DataTable dt = new DataTable();
        DataRow dr;
        string IDs = "1,2,3,4,5,6,7,8,9,10,11,12,13";
        string articles = "Articles1, Articles2, Articles3,Articles4, 
                           Articles5, Articles6, Articles7, Articles8, 
                           Articles9, Articles10, Articles11, Articles12, 
                           Articles13";
        string idSubCategory = "8,2,9,2,7,2,3,4,1,5,6,7,1";
        string[] list1 = IDs.Split(',');
        string[] list2 = articles.Split(',');
        string[] list3 = idSubCategory.Split(',');
        dt.Columns.Add(new DataColumn("nameArticle"));
        dt.Columns.Add(new DataColumn("idArticle"));
        dt.Columns.Add(new DataColumn("idSubCategory"));
        for (int i = 0; i < list1.Length; i++)
        {
            dr = dt.NewRow();
            dr["idArticle"] = list1[i];
            dr["nameArticle"] = list2[i];
            dr["idSubCategory"] = list3[i];
            dt.Rows.Add(dr);
        }
        return dt;
    }