日期:2014-05-18 浏览次数:20768 次
//生成父节点 private void CreateRoot() { DataTable dt = new DataTable(); //adapter.Fill(dt); if(dt.Rows.Count > 0) { foreach(DataRow dr in dt.Rows) { string id = dr["id"].ToString(); string name = dr["name"].ToString(); TreeNode root = new TreeNode(id,name);//也可只用Text,不用Value TreeView1.Nodes.Add(root); AddChildNode();//也可逐步加载,如果节点很多的话 } } } //递归调用生成子节点 private void AddChildNod(TreeNode parentNode) { //添加当前节点的子节点 //TreeNode childNode = new ChildNode(); //parentNode.ChildNodes.Add(childNode); //递归调用,如果下面还有子节点的话 //AddChildNode(childNode); }
------解决方案--------------------
private void InitTree (TreeNodeCollection Nds,string parentId)
{
DataView dv=new DataView();
TreeNode tmpNd;
string intId;
dv.Table=ds.Tables["表名"];
dv.RowFilter="ParentId='"+parentId+"'";
foreach(DataRowView drv in dv)
{
tmpNd=new TreeNode();
tmpNd.Tag=drv["NodeID"].ToString();
tmpNd.Text=drv["Name"].ToString();
Nds.Add(tmpNd);
intId=drv["ParentId"].Tostring();
InitTree(tmpNd.Nodes,tmpNd.Tag);
}
}
NodeID Name ParentId
1 a 0
2 c 0
3 f 1
4 h 2