Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Populating a winforms treeview control using recursion
 

Populating a winforms treeview control using recursion

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 Smile

koenyn  Friday, October 17, 2008 12:04 PM
Hi koenyn,
Thanks for this post . It really helpmed me,cool one!!,
Regards,
Manjunatha.V
vmanzu_1977  Tuesday, April 07, 2009 8:42 AM
Here's the sample code


If its from a database Here's a sample TableDesign


ID Varchar(8)
Data Varchar(50)
ParentID Varchar(8)

Note that There is a parentID. This parent ID links the child node to its parent.
Either Way get the data to a DataSet


Dim oDataSet As New DataSet
Public Sub PopulateTree(ByVal ParentId As String, ByVal TVNode As TreeNode)
Dim oDataView As New DataView(oDataSet.Tables(0), "ParentID='" & ParentId & "'",
"DATA", DataViewRowState.OriginalRows)
Dim oDataRow As DataRowView
For Each oDataRow In oDataView
Dim oTreeNode As New TreeNode(oDataRow("DATA"))
Dim oComboBox As New ComboBox
If TVNode Is Nothing Then
Else
TVNode.Nodes.Add(oTreeNode)
PopulateTree(oDataRow("ID"), oTreeNode)
End If
Next
End Sub


Read In Details

http://muruganad.com/ASP.NET/ASP_.NET_How_to_Populate_a_TreeView_Control_With_TreeNode_s_Using_recursive_algorithm_or_recursion_.html

Thanks

Murugan Andezuthu Dharmaratnam
Murugan Andezuthu Dharmaratnam  Sunday, August 30, 2009 1:26 PM

You can use google to search for other answers

Custom Search

More Threads

• Cells Merging in DataGridView
• TypedDataset - Partial Class - Adding new Column to appear in Bindable controls
• Removing the asterisk
• editing row paste of DataGridView c#
• ConboBox in DataGrid
• Help needed cellpainting with combobox column
• where i can edit update statement for table adapter?
• DataGridView BindingSource Change Position
• deleting a row
• Problem in using Aggregate function with column and expression