开发者

treeview adding node problem

开发者 https://www.devze.com 2023-02-02 07:00 出处:网络
I have one class called Holder (holder.cs) that contain the following: string name; List<String> overView;

I have one class called Holder (holder.cs) that contain the following:

  string name;
  List<String> overView;

Both have get and set accesors.

Now the problem is getting a treeview with all the overView items as parent node, that lists all the names that belong to it below.

So in my treeviewForm.cs i have the following so far to list the overView items properly. The name are entered trough a textbox, and the overView are just 4 items that may or mamye not be selected by choosing the checkbox.

But i have no idea how exactly i can add the names to the overView node. Since i cannot seem to concatenate anything behind treeViewList.Nodes.Add(list[i].overView[j] where the names should come.

listForm.cs class:

    List<Holder> list;

    private void ShowOverviewWithName() 
    {
        treeViewList.Nodes.Clear();

        for (int i=0; i < list.Count; i++) 
        {
            for 开发者_运维问答(int j=0; j < list[i].overView.Count; j++)
            {
                //adds the overView name
                treeViewList.Nodes.Add(list[i].overView[j]); 
            }
        }

        treeViewList.ExpandAll();
    }

So basicly the treeview display i am looking for is:

Overview1
   name1
   name2
   name3
Overview2
   name7
   anyothernamethatbelongshere...

With all the names that belong to the overview.

Thanks.


I'm not sure about your problem...

but supposing to have this code:

var h1 = new Holder{ name = "name1", overView = new List<string>{ "Overview1", "Overview2" } };
var h2 = new Holder{ name = "name2", overView = new List<string>{ "Overview1" } };
var h3 = new Holder{ name = "name3", overView = new List<string>{ "Overview1" } };
var h4 = new Holder{ name = "name4", overView = new List<string>{ "Overview2" } };

List<Holder> list = new List<Holder> { h1, h2, h3, h4 };

treeViewList.Nodes.Clear();

for (int i = 0; i < list.Count; i++)
{
    for (int j = 0; j < list[i].overView.Count; j++)
    {
        string overviewName = list[i].overView[j];

        //adds the overView name if doesn't exist yet
        TreeNode parent;
        if (!treeViewList.Nodes.ContainsKey(overviewName))
            parent = treeViewList.Nodes.Add(overviewName,overviewName);
        else
            parent = treeViewList.Nodes[overviewName];

        // adds the name under the overView
        parent.Nodes.Add(list[i].name);
    }
}

treeViewList.ExpandAll();

you will get the following tree:

treeview adding node problem


You need to:

  1. Create the top level "Overviewx" node
  2. Add the child nodes to the top level node you have just created
  3. Add the top level node to the tree
  4. Repeat for all top level nodes.

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消