Populate treeview from list in C#

孤街浪徒 提交于 2019-12-11 08:21:19

问题


I've made some researches and I found some topic close to my problem, but none of them solved it.

populate treeview from a list of path

http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.aspx

http://social.msdn.microsoft.com/Forums/en/winforms/thread/dae1c72a-dd28-4232-9aa4-5b38705c0a97

SharpSvn: Getting repository structure and individual files

I want to make a repository browser for my SVN folder so that the user can choose one folder and it will be return to a text box.

This is my actual code :

    private void sourceTrunkBrowseButton_Click(object sender, EventArgs e)
     {
         using (SvnClient svnClient = new SvnClient())
         {
             Collection<SvnListEventArgs> contents;
             Collection<SvnListEventArgs> contents2;
             List<TreeItem> files = new List<TreeItem>();
             if (svnClient.GetList(new Uri("https://sourcecode/svn/XXXXXX"), out contents))
             {
                 foreach (SvnListEventArgs item in contents)
                 {
                     if (item.Path != "")
                     {
                         files.Add(new TreeItem(item.Path, 0));

                         if (svnClient.GetList(new Uri("https://sourcecode/svn/XXXXX" + item.Path), out contents2) && item.Path != "")
                         {
                             foreach (SvnListEventArgs item2 in contents2)
                             {
                                 if (item2.Path != "")
                                 {
                                     files.Add(new TreeItem(item2.Path, 1));
                                 }
                             }
                         }
                     }
                 }
             }
             svnBrowser_.FillMyTreeView(files);
             svnBrowser_.Show();
         }
     }

And

  public void FillMyTreeView(List<AutoTrunk.TreeItem> files) 
 {

       // Suppress repainting the TreeView until all the objects have been created.
        svnTreeView.BeginUpdate();

        svnTreeView.Nodes.Clear();
        List<TreeNode> roots = new List<TreeNode>();
         roots.Add(svnTreeView.Nodes.Add("Items"));
         foreach (AutoTrunk.TreeItem item in files)
     {
         if (item.Level == roots.Count) roots.Add(roots[roots.Count - 1].LastNode);
         roots[item.Level].Nodes.Add(item.BrowsePath);
     }

        // Begin repainting the TreeView.
        svnTreeView.EndUpdate();
 }

But my tree look like this :

+---Name1
|   |
|   +------Name2
|   |
|   +------Name3
|   |
|   +------Name5
|   |
|   +------Name6
|
+---Name4

but Name 5 and Name 6 should be under Name 4

Sorry for the long post and thanks!


回答1:


if(item.Level == roots.Count) is your problem I'm thinking... Are you sure the items have the correct level? For example, if Name1 and Name4 are roots, then what happens after you've encountered your second root? Does this give the intended result:

TreeNode root = svnTreeView.Nodes.Add("Items");
TreeNode workingNode = root;
foreach (AutoTrunk.TreeItem item in files)
{
    if (item.Level == 0) 
        workingNode = root.Nodes.Add(item.BrowsePath);
    else
        workingNode.Nodes.Add(item.BrowsePath);
}

Just a thought.



来源:https://stackoverflow.com/questions/10724600/populate-treeview-from-list-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!