Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a database table (named Topics) which includes these fields :

  1. topicId
  2. name
  3. parentId

and by using them I wanna populate a TreeView in c#. How can I do that ?

Thanks in advance...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
438 views
Welcome To Ask or Share your Answers For Others

1 Answer

It will probably be something like this. Give some more detail as to what exactly you want to do if you need more.

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

 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...