0

我有个问题。我正在使用BrightIdeasSoftware.TreeListView在我的 Windows 窗体中创建树视图,并且我还参考了如何在 C# Winforms 应用程序中创建这样的多列树视图,但我没有成功创建它。你能告诉我我在创建树视图时做错了什么吗?完整的描述如下。

  • 我想添加动态可折叠面板并在其中添加树。
  • 数据将根据可折叠面板标题 id 显示到面板中。
  • 我有一个父母 ID,可以有多个孩子。
  • 状态栏将显示一张图片

下面是我用来创建树视图的代码

class Node
        {
            public string Name { get; private set; }
            public string Column1 { get; private set; }
            public string Column2 { get; private set; }

            public List<Node> Children { get; private set; }

            public Node(string name, string col1, string col2)
            {
                this.Name = name;
                this.Column1 = col1;
                this.Column2 = col2;
                this.Children = new List<Node>();
            }
        }



private void InitializeFristTabValue()
    {
        if (_UserDAC == null)
            _UserDAC = new UserDAC();
        try
        {
            DataTable dtUserGroup = _UserDAC.GetAllSECGROUPS(appDirectory, this.FindForm().Name, "InitializeFristTabValue()").Tables[0];
            CommonDataGridBind cc = new CommonDataGridBind();
            cc.GridviewGrouping(dtUserGroup, kryptonOutlookGridUserGroup, true, "GROUPID");
            DataTable SEC_Info = _UserDAC.GetSECAPPINFO(appDirectory, this.FindForm().Name, "InitializeFristTabValue()").Tables[0];
            int Location = 0;
            foreach (DataRow dtrow in SEC_Info.Rows)
            {


                int APPID;
                int.TryParse(dtrow["APPID"].ToString(), out APPID);
                collapsiblePanelobj = new CollapsiblePanel();

                if (Location == 0)
                {
                    collapsiblePanelobj.Collapse = false;
                    //collapsiblePanelobj.Dock = System.Windows.Forms.DockStyle.Fill;
                    this.collapsiblePanelobj.Size = new System.Drawing.Size(1000, 150);
                    //DataTable SEC_InfoTreeView = _UserDAC.GetAPPITEMSbyAPPID(APPID, appDirectory, this.FindForm().Name, "InitializeFristTabValue()").Tables[0];

                    // ADDNode(APPID);
                    treeListView1.Dock = System.Windows.Forms.DockStyle.Fill;
                    //_panelBox.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left;
                    //_panelBox.Controls.Add(treeListView1);
                    //collapsiblePanelobj.Controls.Add(_panelBox);
                    collapsiblePanelobj.Controls.Add(treeListView1);
                }
                else
                    collapsiblePanelobj.Collapse = true;
                Location = Location + 30;
                collapsiblePanelobj.Name = dtrow["APPNAME"].ToString() + dtrow["APPID"].ToString();
                collapsiblePanelobj.HeaderText = "PROGRAM: " + dtrow["APPNAME"].ToString();

                collapsiblePanelobj.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
                collapsiblePanelobj.BackColor = System.Drawing.Color.Transparent;
                collapsiblePanelobj.HeaderCornersRadius = 5;
                collapsiblePanelobj.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold);
                collapsiblePanelobj.HeaderImage = null;
                collapsiblePanelobj.HeaderTextColor = System.Drawing.Color.Black;
                collapsiblePanelobj.Location = new System.Drawing.Point(10, Location);
                collapsiblePanelobj.RoundedCorners = true;
                //this.collapsiblePanelobj.Dock = System.Windows.Forms.DockStyle.Fill;
                this.collapsiblePanelobj.Size = new System.Drawing.Size(1000, 150);
                collapsiblePanelobj.UseAnimation = true;
                InitializeData(APPID);
                FillTree();
                AddTree();

                panel1.Controls.Add(collapsiblePanelobj);
                // GrpBoxUserGroupInfo.Controls.Add(collapsiblePanelobj);
            }
        }
        catch (Exception ex)
        {
            LogsWrite(ex, appDirectory, this.FindForm().Name, "InitializeFristTabValue()");
        }
    }

 public void ADDNode()
        {
            AddTree();

            InitializeData();
            FillTree();

        }

  private void AddTree()
        {
            treeListView1 = new BrightIdeasSoftware.TreeListView();
            treeListView1.Dock = DockStyle.Fill;
            this.Controls.Add(treeListView1);
        }

 private void InitializeData()
        {
            UserDAC _UserDAC = new UserDAC();
            DataTable SEC_InfoTreeView = _UserDAC.GetAPPITEMSbyAPPID(2, "", this.FindForm().Name, "InitializeFristTabValue()").Tables[0];
            data = new List<Node> {};
            var parent1 = new Node("User Name                         ", "State                  ", "Static Caption             ");

                for (int i = 0; i < SEC_InfoTreeView.Rows.Count; i++)
                {
                    int PrentID = Convert.ToInt32(SEC_InfoTreeView.Rows[i]["PARENTID"]);
                    if (PrentID == 0 && Convert.ToInt32(SEC_InfoTreeView.Rows[i]["INDENT"]) == 0)
                    {
                        data.Add(parent1);
                        var parentAdd = new Node(Convert.ToString(SEC_InfoTreeView.Rows[i]["USERNAME"]), "", Convert.ToString(SEC_InfoTreeView.Rows[i]["ACAPTION"]));
                        parent1 = parentAdd;
                    }
                    else if (PrentID != 0 && Convert.ToInt32(SEC_InfoTreeView.Rows[i]["INDENT"]) == 2)
                    {
                        parent1.Children.Add(new Node(Convert.ToString(SEC_InfoTreeView.Rows[i]["USERNAME"]), "", Convert.ToString(SEC_InfoTreeView.Rows[i]["ACAPTION"])));

                    }
                }

        }

 private void FillTree()
    {
       // this.treeListView.SmallImageList = imageList1;
        // set the delegate that the tree uses to know if a node is expandable   
       // treeListView1.Margin = new System.Windows.Forms.Padding(2, 1000, 2, 2);
        treeListView1.CanExpandGetter = x => (x as Node).Children.Count > 0;

        // set the delegate that the tree uses to know the children of a node
        treeListView1.ChildrenGetter = x => (x as Node).Children;

        // create the tree columns and set the delegates to print the desired object proerty
        var nameCol = new BrightIdeasSoftware.OLVColumn("User Name", "Name");
        nameCol.AspectGetter = x => (x as Node).Name;
        nameCol.Width = treeListView1.Width / 3;

        var col1 = new BrightIdeasSoftware.OLVColumn("State", "Column1");
        col1.AspectGetter = x => (x as Node).Column1;
        col1.Width = treeListView1.Width / 3;
        var col2 = new BrightIdeasSoftware.OLVColumn("Static Caption", "Column2");
        col2.AspectGetter = x => (x as Node).Column2;
        col2.Width = treeListView1.Width / 3;
        // add the columns to the tree


        treeListView1.Columns.Add(nameCol);
        treeListView1.Columns.Add(col1);
        treeListView1.Columns.Add(col2);

        // set the tree roots
        this.treeListView1.Roots = data;



    }

在此处输入图像描述

提前感谢您的帮助和评论

4

0 回答 0