使用treeview创建内容表 [英] Create table of content using treeview
问题描述
我正在尝试使用具有父/子关系的树视图创建TOC(内容列表)。
例如,我的数据表就像
ID Parent_Id名称
1 0 AA
2 1 BB
3 1 CC
4 3 CC第三个孩子
5 0 DD
6 5 EE
7 5 FF >
我希望将树节点显示为
AA
------ BB
------ CC
***** ------ CC的第3个孩子/>
DD
------ EE
------ FF
请帮助我先生
I am trying create a TOC(Table Of Content) using a tree view with the parent/child relationship.
For example, my datatable is like
ID Parent_Id Name
1 0 AA
2 1 BB
3 1 CC
4 3 3rd Child Of CC
5 0 DD
6 5 EE
7 5 FF
I want show the tree node as
AA
------BB
------CC
*****------3rd Child Of CC
DD
------EE
------FF
Please help me sir
推荐答案
您可以采取以下步骤使用父/子关系的树视图
1 - 首先在表格或页面的加载事件上绑定父数据
You can take these steps to use tree view with the parent/child relationship
1-First bind parent data on load event of form or page
DataTable dt= new DataTable();
dt= GetMenuData();// here inside get GetMenuData method you can write query like this - select * from yourtablename where parent_id=0
PopulateParentNodes(dt);
2-现在你可以编写一个PopulateParentNodes方法来绑定所有父节点
2- Now you can write a PopulateParentNodes method for binding all parent nodes
private void PopulateParentNodes(DataTable dt)
{
foreach (DataRow row in dt.Rows)
{
TreeNode newNode = new TreeNode(row["Name"].ToString(),
row["Id"].ToString());
TreeView1.Nodes.Add(newNode);
newNode.PopulateOnDemand = true;
}
}
3-接下来,您可以生成事件TreeView1_TreeNodePopulate,以便您可以按需填充子节点 -
3- Next you can generate an event TreeView1_TreeNodePopulate so that you can populate child nodes on demand-
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
DataTable dt= new DataTable();
int id = Convert.ToInt32(e.Node.Value);
dt= objplace.PopulateChildNodes(id);
foreach (DataRow row in dt.Rows)
{
TreeNode newNode = new TreeNode(row["Name"].ToString(),
row["Id"].ToString());
e.Node.ChildNodes.Add(newNode);
}
}
通过使用递归方法,我完成了这个...
首先假设您有数据表中的数据
然后使用此代码
void PopulateMenu()
{
dtTocData = new DataTable();
dtTocData = MacroInfo.dtToc;
if(dtTocData!= null && dtTocData.Rows.Count> 0)
{
DataTable dtParents = GetTOCByParent( 0);
foreach(dtParents.Rows中的DataRow drTOC)
{
AddSubMenu(drTOC,trvToc.Nodes);
}
}
}
private void AddSubMenu(DataRow drTOC,TreeNodeCollection tncTOC)
{
TreeNode oTOCNode;
DataTable dtTOCs = GetTOCByParent(Convert.ToInt32(drTOC [TocId])) ;
oTOCNode = new TreeNode(Convert.ToString(drTOC [Name]),Convert.ToString(drTOC [Name]));
// oTOCNode .Selectable = false;
tncTOC.Add(oTOCNode);
foreach(dtTOCs.Rows中的DataRow drChildRow)
{
AddSubMenu(drChildRow,oTOCNode.ChildNodes);
}
}
私有DataTable GetTOCByParent (int TocId)
{
DataTable dtTOCs = new DataTable();
DataRow [] drTOC = null;
drTOC = dtTocData.Select(ParentId =''+ TocId +'');
if(drTOC.Length> 0)
{
dtTOCs = drTOC.CopyToDataTable();
}
返回dtTOCs;
}
By using a recursion Method I completed this...
First suppose you have the data inside a datatable
Then use this code
void PopulateMenu()
{
dtTocData = new DataTable();
dtTocData = MacroInfo.dtToc;
if (dtTocData!=null && dtTocData.Rows.Count > 0)
{
DataTable dtParents = GetTOCByParent(0);
foreach (DataRow drTOC in dtParents.Rows)
{
AddSubMenu(drTOC, trvToc.Nodes);
}
}
}
private void AddSubMenu(DataRow drTOC, TreeNodeCollection tncTOC)
{
TreeNode oTOCNode;
DataTable dtTOCs = GetTOCByParent(Convert.ToInt32(drTOC["TocId"]));
oTOCNode = new TreeNode(Convert.ToString(drTOC["Name"]),Convert.ToString(drTOC["Name"]));
//oTOCNode.Selectable = false;
tncTOC.Add(oTOCNode);
foreach (DataRow drChildRow in dtTOCs.Rows)
{
AddSubMenu(drChildRow, oTOCNode.ChildNodes);
}
}
private DataTable GetTOCByParent(int TocId)
{
DataTable dtTOCs = new DataTable();
DataRow[] drTOC = null;
drTOC = dtTocData.Select("ParentId=''" + TocId + "''");
if (drTOC.Length > 0)
{
dtTOCs = drTOC.CopyToDataTable();
}
return dtTOCs;
}
这篇关于使用treeview创建内容表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!