treeview控件添加节点问题,大家帮帮忙,急啊!
树的代码如下
-----------------------------------------------------
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
if (e.Node.ChildNodes.Count == 0)
{
int id = 0;
id = Convert.ToInt32(e.Node.Value);
CreateTree(e.Node.ChildNodes, id);
}
}
/// <summary>
/// 创建树
/// </summary>
/// <param name="nodes"></param>
/// <param name="p_id"></param>
public void CreateTree(TreeNodeCollection nodes, int p_id)
{
nodes.Clear();
IList<OATree> oaTreeList = oaTreeService.GetOATreeByParentId(p_id);
if (oaTreeList != null && oaTreeList.Count > 0)
{
for (int i = 0; i < oaTreeList.Count; i++)
{
OATree oaTreeObj = new OATree();
oaTreeObj = (OATree)oaTreeList[i];
nodes.Add(new TreeNode(oaTreeObj.NodeName, oaTreeObj.NodeID.ToString()));
//nodes[i].CollapseAll(); //折叠树
nodes[i].SelectAction = TreeNodeSelectAction.Select;
nodes[i].PopulateOnDemand = true;
nodes[i].ToolTip = oaTreeObj.NodeName;
nodes[i].Expand();
}
}
TreeNode tnode = TreeView1.FindNode(hf_nodePath.Value);
if (tnode != null)
tnode.Selected = true;
}
------------------------------------------------
创建出来的树样子如下
tree1
|____
tree2
|____
tree3
|____
也就是有三个树的节点
我想手动加一个顶级节点变成如下效果
treeview
|___tree1
|___tree2
|___tree3
大家帮帮忙。。小弟不才在此谢了!
------解决方案--------------------在TreeView外部可以通过下面的方式处理:
TreeView.Nodes.Add() - 添加根节点
TreeView.Nodes[i].ChildNodes.Add() - 添加子节点
在你的事件里,自己改一下吧
------解决方案--------------------C# code
TreeNode tn = TreeView1.Nodes[0];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TreeNode YearNode = new TreeNode(ds.Tables[0].Rows[i]["Years"].ToString(), ds.Tables[0].Rows[i]["Years"].ToString());
YearNode.Text = " " + ds.Tables[0].Rows[i]["Years"].ToString() + "年";
for (int j = 1; j < 13; j++)
{
TreeNode MonthNode = new TreeNode(j.ToString(), j.ToString());
MonthNode.Text = " " + j.ToString() + "月";
YearNode.ChildNodes.Add(MonthNode);
}
tn.ChildNodes.Add(YearNode);
}