从C#中的列表填充treeview [英] Populate treeview from list in C#

查看:386
本文介绍了从C#中的列表填充treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了一些研究,发现一些与我的问题很接近的话题,但没有一个人解决了.

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

从路径列表中填充树视图

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

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

SharpSvn:获取存储库结构和单个文件

我想为我的SVN文件夹创建一个存储库浏览器,以便用户可以选择一个文件夹,它将返回到文本框.

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.

这是我的实际代码:

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

还有

  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

但名称5和名称6应该在名称4下

but Name 5 and Name 6 should be under Name 4

很抱歉,很长的帖子,谢谢!

Sorry for the long post and thanks!

推荐答案

if(item.Level == roots.Count)是您在考虑的问题吗?您确定这些项目的级别正确吗?例如,如果Name1Name4是根,那么遇到第二个根后会发生什么?这会达到预期的结果吗?

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

只是一个想法.

这篇关于从C#中的列表填充treeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆