Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello everyone,

How to fill Tree view with data from gridview or dt,

Data table has 4 columns:

1- Parent ID
2- Parent Name
3- Child ID
4- Child Name

What I have tried:

Almost all of the examples that i found where static trees, hard coded so this won't be use full in my scenario
Posted
Updated 15-May-16 4:20am
v2
Comments
Sergey Alexandrovich Kryukov 15-May-16 10:58am    
Why parent ID and child ID, not just ID? Each parent can be some child.
—SA
Member 12163718 16-May-16 1:18am    
I was thinking about linking the IDs together
Sergey Alexandrovich Kryukov 16-May-16 2:27am    
It doesn't answer my question.
—SA

1 solution

This is a pretty tall order for the Quick Answer forum.
If you paid enough I would happily write the code for you. :)

I assume you get the data from the database into one or more data tables.
As a start you can look at these articles:
Create Treeview From Datatable In C#| Dynamic Treeview Control In C# | Treeview Nodes In C# |How To Bind Nodes To Treeview Control At Runtime In C# | Author Code | AuthorCode[^]

[UPDATE]
Seems to be a bug in part of the code from the link above.
You will get the compiler error "Not all code paths return a value."
See the fix below.
C#
private TreeNode Searchnode(string nodetext, TreeView trv)
{
    foreach (TreeNode node in trv.Nodes) 
    {
        if (node.Text == nodetext) 
        {
            return node;
        }
    }

    // Added the line below, so the method returns null if no node was found
    return null; 
}

Then you have to take care of the case that the method can return null.


Populate a TreeView Control C#[^]

As for drag & drop, look here: Dragging tree nodes in C#[^]

These articles should get you started.
 
Share this answer
 
v2
Comments
Member 12163718 15-May-16 7:13am    
This is my first post ever so i am getting around the posting thing :)

Thank you George for the provided links, i will look into them
George Jonsson 15-May-16 7:50am    
You are welcome and also welcome to the forum.
Learning by doing is the key.
Member 12163718 16-May-16 1:40am    
I tried the first link and i am getting the following message "not all code paths return a value", any ideas to why i am seeing this message ?
George Jonsson 16-May-16 1:52am    
It is because of the method Searchnode(). If no node is found, then there is no return value.
See my updated answer.
Member 12163718 16-May-16 2:16am    
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
CreateDataTable();
}

public DataTable CreateDataTable()
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Country");
// The value in this column will display on the TreeNode
dataTable.Columns.Add("City");
// The value in this column will identify its parentId

// Fill the DataTable
dataTable.Rows.Add("India", "New Delhi");
dataTable.Rows.Add("India", "Mumbai");
dataTable.Rows.Add("India", "Kolkata");
dataTable.Rows.Add("India", "Noida");
dataTable.Rows.Add("USA", "New York");
dataTable.Rows.Add("USA", "Washington");
dataTable.Rows.Add("USA", "");
dataTable.Rows.Add("USA", "India");
return dataTable;
}

public void BuildTree(DataTable dt, TreeView trv, Boolean expandAll)
{
// Clear the TreeView if there are another datas in this TreeView
trv.Nodes.Clear();
TreeNode node = default(TreeNode);
TreeNode subNode = default(TreeNode);
foreach (DataRow row in dt.Rows)
{
//search in the treeview if any country is already present
node = Searchnode(row[0].ToString(), trv);
if (node != null)
{
//Country is already present
subNode = new TreeNode(row[1].ToString());
//Add cities to country
node.Nodes.Add(subNode);
}
else
{
node = new TreeNode(row[0].ToString());
subNode = new TreeNode(row[1].ToString());
//Add cities to country
node.Nodes.Add(subNode);
trv.Nodes.Add(node);
}
}
if (expandAll)
{
// Expand the TreeView
trv.ExpandAll();
}
}
public TreeNode Searchnode(string nodetext, TreeView trv)
{
foreach (TreeNode node in trv.Nodes)
{
if (node.Text == nodetext)
{
return node;
}
}

return null;
}
}


I need to call BuildTree in order to have a value isn't that correct ? since the value returned is null in this case

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900