在C#中怎样给TreeView控件循环的动态的添加子节点

给TreeView控件循环的动态的添加子节点
2024-12-22 17:04:07
推荐回答(4个)
回答1:

private void bindtreeview()
{
this.TreeView1.Nodes.Clear();
string strsql = "select * from title,detitle where title.title_id=detitle.title_id";
DataSet ds = DB.ds(strsql);
this.ViewState["DataSet_Tree"] = ds;
addtree(0, (TreeNode)null);
}

public void addtree(int ParentId, TreeNode pnode)
{
DataSet DataSet_Tree = (DataSet)this.ViewState["DataSet_Tree"];
DataView DataView_Tree = new DataView(DataSet_Tree.Tables[0]);
DataView_Tree.RowFilter = "title_id=" + ParentId;
foreach (DataRowView row in DataView_Tree)
{
TreeNode Node = new TreeNode();
if (pnode == null)
{
Node.Text = row["title_name"].ToString();
Node.Value = row["title_id"].ToString();
this.TreeView1.Nodes.Add(Node);
Node.Expanded = true;//展开当前节点
addtree(Int32.Parse(row["title_id"].ToString()), Node);
}
else
{
Node.Text = row["detitle_name"].ToString();
Node.Value=row["detitle_id"].ToString();
pnode.ChildNodes.Add(Node);
Node.Expanded = true;
addtree(Int32.Parse(row["detitle_id"].ToString()), Node);
}
}

回答2:

public void CreateNode(String parentno, TreeNode parentnode, DataTable dt)
{
DataRow[] drs = dt.Select("ParentNo= " + parentno);

foreach (DataRow r in drs)
{
string nodeid = r["GroupNo"].ToString();
string text = r["GroupName"].ToString();
TreeNode node = new TreeNode();
node.Text = text;
node.Value = nodeid;
parentnode.ChildNodes.Add(node);
CreateNode(parentno,node,dt);
}
}
}

回答3:

控件名.Nodes.add或者是控件名.ChlidNodes.add具体哪个忘记了

回答4:

可以绑定数据源