Ok, so I know its not one of the more popular controls in windows forms applications, and with the new WPF treeview control databinding is now available, but its very frustrating seeing all these silly people who use for loops to populate this control instead of recursion. Plus Recursion is a lot simpler to implement. So here is my coding example:
private void
RefreshTvNodes()
{
DataSet
dsList = new DataSet();
BLL.NodeTreeViewList(ref dsList);
tvNodes.Nodes.Clear();
TreeNode tnRoot = new TreeNode("Root Node");
tnRoot.Name = "0";
AddChildNodes(tnRoot, 0, dsList);
tvNodes.Nodes.Add(tnRoot);
}
private void AddChildNodes(TreeNode tnParentNode, int parentId, DataSet dsList)
{
foreach (DataRow drRow in dsList.Tables[0].Select("parent_Id = " + parentId))
{
TreeNode tnChildNode = new TreeNode(drRow["Desc"].ToString());
tnChildNode.Name = Convert.ToInt32(drRow["node_Id"]).ToString();
tnParentNode.Nodes.Add(tnChildNode);
AddChildNodes(tnChildNode, Convert.ToInt32(drRow["node_Id"]), dsList);
}
}
this really is the simplest solution and also you can then use the ID of your item in the tree to do modifications and loading of the data by simply using the following statement :
int node_Id = Convert.ToInt32(tvNodes.SelectedNode.Name);
hope this helps someone 