【问题标题】:how can we get a treeview structure with nultiple childnodes我们如何获得具有多个子节点的树视图结构
【发布时间】:2016-07-24 13:27:22
【问题描述】:

在我的应用程序中,我需要带有子节点的树视图结构,应用程序应该采用这样的方式,即我可以拥有多个没有节点的子节点。当用户点击这些节点时,它应该可以展开我该怎么做

【问题讨论】:

  • 你有一个 SQL 注入漏洞。
  • 您看到Columns 属性了吗?

标签: c# asp.net datatable treeview


【解决方案1】:

如果我理解您的意思,您可以添加一个额外的节点作为标题,例如:

private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
    {


        foreach (DataRow row in dtParent.Rows)
        {
            string Value = string.Empty;
            string header = string.Empty;
            // this is just extra node for header
            if (dtParent.Columns.Count > 2)
            {
                header += "Name" + " " + " ";
                header += "VehicleType" + " " + " ";
                header += "Capacity" + " " + " ";
            }
            else
            {
                header += "Name" + " " + " ";
            }
            TreeNode headerNode = new TreeNode
            {
                Text = header.ToString(),
                 // Any value that you want and has no confelict with other
                Value = row["Id"].ToString()

            };


            if (dtParent.Columns.Count > 2)
            {
                Value += row["Name"].ToString() + " " + " ";
                Value += row["VehicleType"].ToString() + " " + " ";
                Value += row["Capacity"].ToString() + " " + " ";
            }
            else
            {
                Value += row["Name"].ToString() + " " + " ";
            }

            TreeNode child = new TreeNode
            {
                Text = Value.ToString(),
                Value = row["Id"].ToString()

            };



            if (parentId == 0)
            {
                TreeView1.Nodes.Add(child);
                DataTable dtChild = this.GetData("SELECT Id, Name,VehicleType,Capacity FROM Bikes WHERE VehicleTypeId = " + child.Value);
                PopulateTreeView(dtChild, int.Parse(child.Value), child);
            }
            else
            {
                treeNode.ChildNodes.Add(child);
            }
        }
    }

或者如果你的意思是找到每个列名,你可以使用这个:

private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
    {


        foreach (DataRow row in dtParent.Rows)
        {
            string Value = string.Empty;
            string header = string.Empty;
            // Get all columns name
            for (var i=0;i<(dtParent.Columns.Count)-1;i++)
            {
                header += dtParent.Columns[i].ColumnName + "  ";
            }

            TreeNode headerNode = new TreeNode
            {
                Text = header.ToString(),
                 // Any value that you want and has no confelict with other
                Value = row["Id"].ToString()

            };


            if (dtParent.Columns.Count > 2)
            {
                Value += row["Name"].ToString() + " " + " ";
                Value += row["VehicleType"].ToString() + " " + " ";
                Value += row["Capacity"].ToString() + " " + " ";
            }
            else
            {
                Value += row["Name"].ToString() + " " + " ";
            }

            TreeNode child = new TreeNode
            {
                Text = Value.ToString(),
                Value = row["Id"].ToString()

            };



            if (parentId == 0)
            {
                TreeView1.Nodes.Add(child);
                DataTable dtChild = this.GetData("SELECT Id, Name,VehicleType,Capacity FROM Bikes WHERE VehicleTypeId = " + child.Value);
                PopulateTreeView(dtChild, int.Parse(child.Value), child);
            }
            else
            {
                treeNode.ChildNodes.Add(child);
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2011-09-10
    • 2013-05-18
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多