treeview遞歸綁定的兩種辦法。本站提示廣大學習愛好者:(treeview遞歸綁定的兩種辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是treeview遞歸綁定的兩種辦法正文
辦法一:
public void creattree(int fid, TreeNode parentnode)
{
DataTable dt = new DataTable();
dt = op.BindGroup();
DataRow[] drs = dt.Select("parentid=" + fid);
foreach (DataRow row in drs)
{
if (parentnode != null)
{
TreeNode tempnode = new TreeNode();
tempnode.Text = row["groupname"].ToString();
tempnode.Name = row["groupid"].ToString();
parentnode.Nodes.Add(tempnode);
creattree(Convert.ToInt32(row["groupid"]), tempnode);
}
else
{
TreeNode tempnode = new TreeNode();
tempnode.Text = row["groupname"].ToString();
tempnode.Name = row["groupid"].ToString();
treeView1.Nodes.Add(tempnode);
creattree(Convert.ToInt32(row["groupid"]), tempnode);
}
}
}
private void bindtree()
{
treeView1.Nodes.Clear();
creattree(0,(TreeNode)null);
}
辦法二:
public void AddTree(int ParentID, TreeNode pNode)
{
DataTable dt = new DataTable();
dt = op.BindGroup();
DataView dvTree = new DataView(dt);
//過濾ParentID,獲得以後的一切子節點
dvTree.RowFilter = "parentid = " + ParentID;
foreach (DataRowView Row in dvTree)
{
TreeNode Node = new TreeNode();
if (pNode == null)
{ //添加根節點
Node.Text = Row["groupname"].ToString();
treeView1.Nodes.Add(Node);
//Node.Expanded = true;
AddTree(Int32.Parse(Row["groupid"].ToString()), Node); //再次遞歸
}
else
{ //添加以後節點的子節點
Node.Text = Row["groupname"].ToString();
pNode.Nodes.Add(Node);
//Node.Expanded = true;
AddTree(Int32.Parse(Row["groupid"].ToString()), Node); //再次遞歸
}
}
}
private void bindtree()
{
treeView1.Nodes.Clear();
AddTree(0, (TreeNode)null);
}