动态创建树视图 [英] Dynamically create treeview
本文介绍了动态创建树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示一个分层结构,我希望使用asp.net在vb.net中重新启动treeview这个结构
I want to display a hierarchical structure, i want retrrive the treeview this structure in vb.net using asp.net
__ID__ | __NAME__ | __PARENT__|
1 | Patrick | primary
2 | Mark | primary
3 | Scott | Mark
4 | Jason | Patrick
5 | Julian | Primary
6 | John | Scott
7 | Steve | Scott
8 | George | Patrick
9 | Robert | George
10 | Rodney | George
删除了SHOUTING,添加了代码块 - OriginalGriff [/ edit]
[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]
推荐答案
在VB中查看
See in VB
Private Sub BindTreeViewControl()
Try
Dim dt As New DataTable()
'Assign your database value to dt table where dt is null now
Dim dtParent As DataTable = (From f In dt.AsEnumerable() Where f.Field(Of String)("PARENT").ToLower() = "primary"f).ToList().CopyToDataTable()
For i As Integer = 0 To dtParent.Rows.Count - 1
Dim root As New TreeNode(dtParent.Rows(i)("NAME").ToString(), dtParent.Rows(i)("ID").ToString())
root.SelectAction = TreeNodeSelectAction.Expand
CreateNode(root, dt)
'Sending table
TreeView1.Nodes.Add(root)
Next
Catch Ex As Exception
Throw Ex
End Try
End Sub
Public Sub CreateNode(node As TreeNode, Dt As DataTable)
Dim dtChild As New DataTable()
Dim varChiled = (From f In Dt.AsEnumerable() Where f.Field(Of String)("PARENT").ToLower() = node.Text.ToLower()f).ToList()
If varChiled.Count > 0 Then
dtChild = varChiled.CopyToDataTable()
End If
If dtChild.Rows.Count = 0 Then
Return
End If
For i As Integer = 0 To dtChild.Rows.Count - 1
Dim Childnode As New TreeNode(dtChild.Rows(i)("NAME").ToString(), dtChild.Rows(i)("ID").ToString())
Childnode.SelectAction = TreeNodeSelectAction.Expand
node.ChildNodes.Add(Childnode)
CreateNode(Childnode, Dt)
Next
End Sub
看看这里:演练:显示分层TreeView控件中的数据 [ ^ ]
看!
为它做了代码
See !
did the code for it
private void BindTreeViewControl()
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("NAME", typeof(string));
dt.Columns.Add("PARENT", typeof(string));
dt.Rows.Add(1, "Patrick ", "primary");
dt.Rows.Add(2, "Mark", "primary");
dt.Rows.Add(3, "Scott", "Mark");
dt.Rows.Add(4, "Jason", "Patrick");
dt.Rows.Add(5, "Julian", "Primary");
dt.Rows.Add(6, "John", "Scott");
dt.Rows.Add(7, " Steve", "Scott");
dt.Rows.Add(8, " George", "Patrick");
dt.Rows.Add(9, "Robert", "George");
dt.Rows.Add(10, "Rodney", "George");
// DataRow[] Rows = dt.Select("ParentId IS NULL"); Get all parents nodes
DataTable dtParent = (from f in dt.AsEnumerable()
where f.Field<string>("PARENT").ToLower() == "primary"
select f).ToList().CopyToDataTable();
for (int i = 0; i < dtParent.Rows.Count; i++)
{
TreeNode root = new TreeNode(dtParent.Rows[i]["NAME"].ToString(), dtParent.Rows[i]["ID"].ToString());
root.SelectAction = TreeNodeSelectAction.Expand;
CreateNode(root, dt);//Sending table
TreeView1.Nodes.Add(root);
}
}
catch (Exception Ex) { throw Ex; }
}
public void CreateNode(TreeNode node, DataTable Dt)
{
DataTable dtChild = new DataTable();
var varChiled = (from f in Dt.AsEnumerable()
where f.Field<string>("PARENT").ToLower() == node.Text.ToLower()
select f).ToList();
if (varChiled.Count > 0) dtChild = varChiled.CopyToDataTable();
if (dtChild.Rows.Count == 0) { return; }
for (int i = 0; i < dtChild.Rows.Count; i++)
{
TreeNode Childnode = new TreeNode(dtChild.Rows[i]["NAME"].ToString(), dtChild.Rows[i]["ID"].ToString());
Childnode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(Childnode);
CreateNode(Childnode, Dt);
}
}
这篇关于动态创建树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文