treeview递归绑定的两种方法
程序员文章站
2023-12-18 21:16:10
方法一:
复制代码 代码如下:public void creattree(int fid, treenode parentnode) &...
方法一:
复制代码 代码如下:
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);
}