0

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

  • Category( idCategory, nameCategory)
  • subCategory( idSubCategory, nameSubCategorie, idCategory)
  • Articles( idArticle, 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

用于从数据库 usinf LINQ 绑定递归 treeView 的模板代码或显示如何执行此操作的教程链接将非常有帮助!谢谢。

4

1 回答 1

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;
    }
于 2013-05-21T09:33:43.597 回答