当前位置:  开发笔记 > 编程语言 > 正文

从DataBase填充TreeView

如何解决《从DataBase填充TreeView》经验,为你挑选了5个好方法。

我有一个数据库表(名为Topics),其中包括以下字段:

    topicId

    名称

    parentId的

通过使用它们,我想在c#中填充TreeView.我怎样才能做到这一点 ?

提前致谢...



1> Bob..:

它可能会是这样的.如果您需要更多,请详细说明您想要做什么.

//In Page load
foreach (DataRow row in topics.Rows)
{
    TreeNode node = new TreeNode(dr["name"], dr["topicId"])
    node.PopulateOnDemand = true;

     TreeView1.Nodes.Add(node);
 }
 ///
 protected void PopulateNode(Object sender, TreeNodeEventArgs e)
 {
     string topicId = e.Node.Value;
     //select from topic where parentId = topicId.
     foreach (DataRow row in topics.Rows)
     {
         TreeNode node = new TreeNode(dr["name"], dr["topicId"])
         node.PopulateOnDemand = true;

         e.Node.ChildNodes.Add(node);
     }

 }



2> Brody..:

不完全的.

树木通常不会一次性加载所有东西,因此处理得最好.因此,您需要获取没有parentID的根节点(或主题).然后将它们添加到树根节点,然后对于添加的每个节点,您需要获取其子节点.

foreach (DataRow row in topicsWithOutParents.Rows)
{
   TreeNode node = New TreeNode(... whatever);
   DataSet childNodes = GetRowsWhereParentIDEquals(row["topicId"]);
   foreach (DataRow child in childNodes.Rows)
   { 
       Treenode childNode = new TreeNode(..Whatever);
       node.Nodes.add(childNode);
   }
   Tree.Nodes.Add(node);
}



3> MAK..:

这段代码对我运行完美,看看它我认为它会帮助你:)

;

protected void Page_Load(object sender, EventArgs e)
{
    DataSet ds = RunQuery("Select topicid,name from Topics where Parent_ID IS NULL");
       for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
       { 
           TreeNode root = new TreeNode(ds.Tables[0].Rows[i][1].ToString(),ds.Tables[0].Rows[i][0].ToString());
           root.SelectAction = TreeNodeSelectAction.Expand;
           CreateNode(root);
           TreeView1.Nodes.Add(root);
       }



}
void CreateNode(TreeNode node)
{
    DataSet ds = RunQuery("Select topicid, name from Category where Parent_ID =" + node.Value);
    if (ds.Tables[0].Rows.Count == 0)
    {
        return;
    }
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        TreeNode tnode = new TreeNode(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString());
        tnode.SelectAction = TreeNodeSelectAction.Expand;
        node.ChildNodes.Add(tnode);
        CreateNode(tnode);
    }

}
DataSet RunQuery(String Query)
{
    DataSet ds = new DataSet();
    String connStr = "???";//write your connection string here;
    using (SqlConnection conn = new SqlConnection(connStr))
    {
        SqlCommand objCommand = new SqlCommand(Query, conn);
        SqlDataAdapter da = new SqlDataAdapter(objCommand);
        da.Fill(ds);
        da.Dispose();
    }
    return ds;
}



4> 小智..:
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        PopulateRootLevel();
}


private void PopulateRootLevel()
{
    SqlConnection objConn = new SqlConnection(connStr);
    SqlCommand objCommand = new SqlCommand(@"select FoodCategoryID,FoodCategoryName,(select count(*) FROM FoodCategories WHERE ParentID=c.FoodCategoryID) childnodecount FROM FoodCategories c where ParentID IS NULL", objConn);
    SqlDataAdapter da = new SqlDataAdapter(objCommand);
    DataTable dt = new DataTable();
    da.Fill(dt);
    PopulateNodes(dt, TreeView2.Nodes);
}

private void PopulateSubLevel(int parentid, TreeNode parentNode)
{
    SqlConnection objConn = new SqlConnection(connStr);
    SqlCommand objCommand = new SqlCommand(@"select FoodCategoryID,FoodCategoryName,(select count(*) FROM FoodCategories WHERE ParentID=sc.FoodCategoryID) childnodecount FROM FoodCategories sc where ParentID=@parentID", objConn);
    objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid;
    SqlDataAdapter da = new SqlDataAdapter(objCommand);
    DataTable dt = new DataTable();
    da.Fill(dt);
    PopulateNodes(dt, parentNode.ChildNodes);
}


protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
    PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
}

private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
    foreach (DataRow dr in dt.Rows)
    {
        TreeNode tn = new TreeNode();
        tn.Text = dr["FoodCategoryName"].ToString();
        tn.Value = dr["FoodCategoryID"].ToString();
        nodes.Add(tn);

        //If node has child nodes, then enable on-demand populating
        tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
    }
}



5> Brij..:

当没有大量数据时,连接数据库,获取数据并一次又一次地为子/子节点添加到树视图节点是不好的.它可以一次完成.请参阅以下示例:http:
//urenjoy.blogspot.com/2009/08/display-hierarchical-data-with-treeview.html

推荐阅读
周扒pi
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有