递归生成treeView问题
有一个递归,教我一下怎么写,我有一个表,现在要建一个treeNode,现在要自动生成,表里有两字段,一个是id 一个是nodeID,只要某条数据nodeID等于某条数据id那么,这个数据是那个数据的子节点,我知道应该要递归算法,现在没有头绪
[解决办法]
private void InitTree(TreeNodeCollection Nds,int parentid) { DataView dv = new DataView(); TreeNode tmpNd = null; dv.Table = ds.Tables[0]; dv.RowFilter = String.Format("parentid={0}", parentid); foreach (DataRowView drv in dv) { tmpNd = new TreeNode(); tmpNd.Text = (string)drv["sortname"];//节点名称 tmpNd.NavigateUrl = String.Format("?id={0}", drv["id"]);//节点URL //tmpNd.ImageUrl = ""; //节点图片 if (parentid == 0) tmpNd.Expanded = true; else tmpNd.Expanded = false; Nds.Add(tmpNd); InitTree(Nds[Nds.Count - 1].ChildNodes, (int)drv["id"]); } }
[解决办法]
/// <summary> /// 动态加载步骤序号 /// </summary> private void CreateTreeNode() { CommonExecute comm = new CommonExecute(m_ConnectionString); string strCondition = string.Format("planinfono='{0}'", m_strCondition[0].Trim()); DataSet ds = null; int count = 0; if (comm.GetInfosDataSetByCondition("emergenceprocess", strCondition, "processno asc", out ds) == true)/////// { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { TreeNode root = new TreeNode(); if (!string.IsNullOrEmpty(ds.Tables[0].Rows[i]["processno"].ToString().Trim())) { count++; TreeNode[] childNode = new TreeNode[4]; root.Text = ds.Tables[0].Rows[i]["processno"].ToString(); treeView1.Nodes.Add(root.Text, "步骤" + count); } } } }
[解决办法]
//写一段伪代码吧private void InitTree(){ TreeNode root = new TreeNode("测试树"); root.Name = "root"; //root.Tag root.ImageKey ... this.TreeView1.Nodes.Add(root); DataTable dt = new DataTable(); //dt = ....,设有部门和职员 for(int r = 0; r < dt.Rows.Count; r++) { TreeNode pNode = new TreeNode(dt.Rows[r]["Dep"].ToString()); //pNode.Tag pNode.ImageKey AddChildNode(root,pNode); // }}prrivate void AddChildNode(TreeNode pNode,TreeNode childNode){ pNode.Nodes.Add(childNode); //....... //TreeNode cNode = new TreeNode(); //AddChildNode(childNode,cNode);}
[解决办法]
/// <summary>
/// TreeView和移动List递归添加子节点--nhapis
/// </summary>
/// <param name="node">TreeView</param>
/// <param name="tsi">MoveList</param>
/// <param name="nodeID">节点名称</param>
protected void TreeViewAndMoveList_AddChildNodes(TreeNode node, ToolStripMenuItem tsi, string nodeID)
{
DataSet ds = listBll.getDataSet(" list_group = '" + nodeID + "'");
DataView dv = ds.Tables[0].DefaultView;
foreach (DataRowView row in dv)
{
string unitName = row["list_nm"].ToString().Trim();
string unitID = row["LIST_CONTENT_ID"].ToString().Trim();
TreeNode unit = new TreeNode();
unit.Expand();
unit.Name = unitID;
unit.Text = unitName;
ToolStripMenuItem tsiUnit = new ToolStripMenuItem();
tsiUnit.Name = unitID;
tsiUnit.Text = unitName;
tsiUnit.Image = map;
tsiUnit.Click += tsiUnit_Click;
TreeViewAndMoveList_AddChildNodes(unit, tsiUnit, unitID);
node.Nodes.Add(unit);
tsi.DropDownItems.Add(tsiUnit);
}
}
C# WPF Silverlight开发 共同学习.共同进步..新建群.欢迎加盟..群号:26036666