Populate Multi-Level (N-Level) TreeView using C# and VB.Net in Windows Application

Last Reply one year ago By pandeyism

Posted one year ago

With reference to the below code, the treeview has just two nodes.

Populate TreeView from Database in Windows Forms using C# and VB.Net

I want to add three or four nodes to it.

How will I go about it?

Posted one year ago

Hi dnnyobi,

Refer below sample.

Code

C#

DataTable dtSource = null;
private void Form1_Load(object sender, EventArgs e)
{
    dtSource = GetData();
    DataTable dt = GetChildData(-1);
    foreach (DataRow dr in dt.Rows)
    {
        TreeNode parentNode = new TreeNode();
        parentNode.Text = dr["NodeText"].ToString();
        parentNode.Name = dr["NodeID"].ToString();
        AddNodes(ref parentNode);
        treeView1.Nodes.Add(parentNode);
    }
}

private void AddNodes(ref TreeNode node)
{
    DataTable dt = GetChildData(Convert.ToInt32(node.Name));
    foreach (DataRow row in dt.Rows)
    {
        TreeNode childNode = new TreeNode();
        childNode.Name = row["NodeID"].ToString();
        childNode.Text = row["NodeText"].ToString();
        AddNodes(ref childNode);
        node.Nodes.Add(childNode);
    }
}

public DataTable GetChildData(int parentId)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("NodeId", typeof(int)), new DataColumn("ParentId", typeof(int)), new DataColumn("NodeText") });
    foreach (DataRow dr in dtSource.Rows)
    {
        if (dr[1].ToString() != parentId.ToString())
        {
            continue;
        }
        DataRow row = dt.NewRow();
        row["NodeId"] = dr["NodeId"];
        row["ParentId"] = dr["ParentId"];
        row["NodeText"] = dr["NodeText"];
        dt.Rows.Add(row);
    }

    return dt;
}

private DataTable GetData()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("NodeId", typeof(int)), new DataColumn("ParentId", typeof(int)), new DataColumn("NodeText") });
    dt.Rows.Add(0, -1, "Cars");
    dt.Rows.Add(1, -1, "Bikes");
    dt.Rows.Add(2, 1, "Avenger");
    dt.Rows.Add(3, 1, "Unicorn");
    dt.Rows.Add(4, 0, "Alto");
    dt.Rows.Add(5, 0, "Wagon R");
    dt.Rows.Add(6, 4, "Alto LXI");
    dt.Rows.Add(7, 4, "Alto VXI");
    dt.Rows.Add(8, 5, "Wagon R LXI");
    dt.Rows.Add(9, 5, "Wagon R ZXI");
    dt.Rows.Add(10, 6, "Rs.3.75 Lakh");
    dt.Rows.Add(11, 7, "Rs.3.85 Lakh");
    dt.Rows.Add(12, 8, "Rs.4.34 Lakh");
    dt.Rows.Add(13, 9, "Rs.5.44 Lakh");
    dt.Rows.Add(14, 2, "Avenger Cruise");
    dt.Rows.Add(15, 14, "Rs.1.03 Lakh");
    return dt;
}

VB.Net

Private dtSource As DataTable = Nothing
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    dtSource = GetData()
    Dim dt As DataTable = GetChildData(-1)

    For Each dr As DataRow In dt.Rows
        Dim parentNode As TreeNode = New TreeNode()
        parentNode.Text = dr("NodeText").ToString()
        parentNode.Name = dr("NodeID").ToString()
        AddNodes(parentNode)
        treeView1.Nodes.Add(parentNode)
    Next
End Sub

Private Sub AddNodes(ByRef node As TreeNode)
    Dim dt As DataTable = GetChildData(Convert.ToInt32(node.Name))

    For Each row As DataRow In dt.Rows
        Dim childNode As TreeNode = New TreeNode()
        childNode.Name = row("NodeID").ToString()
        childNode.Text = row("NodeText").ToString()
        AddNodes(childNode)
        node.Nodes.Add(childNode)
    Next
End Sub

Public Function GetChildData(ByVal parentId As Integer) As DataTable
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("NodeId", GetType(Integer)), New DataColumn("ParentId", GetType(Integer)), New DataColumn("NodeText")})

    For Each dr As DataRow In dtSource.Rows

        If dr(1).ToString() <> parentId.ToString() Then
            Continue For
        End If

        Dim row As DataRow = dt.NewRow()
        row("NodeId") = dr("NodeId")
        row("ParentId") = dr("ParentId")
        row("NodeText") = dr("NodeText")
        dt.Rows.Add(row)
    Next

    Return dt
End Function

Private Function GetData() As DataTable
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("NodeId", GetType(Integer)), New DataColumn("ParentId", GetType(Integer)), New DataColumn("NodeText")})
    dt.Rows.Add(0, -1, "Cars")
    dt.Rows.Add(1, -1, "Bikes")
    dt.Rows.Add(2, 1, "Avenger")
    dt.Rows.Add(3, 1, "Unicorn")
    dt.Rows.Add(4, 0, "Alto")
    dt.Rows.Add(5, 0, "Wagon R")
    dt.Rows.Add(6, 4, "Alto LXI")
    dt.Rows.Add(7, 4, "Alto VXI")
    dt.Rows.Add(8, 5, "Wagon R LXI")
    dt.Rows.Add(9, 5, "Wagon R ZXI")
    dt.Rows.Add(10, 6, "Rs.3.75 Lakh")
    dt.Rows.Add(11, 7, "Rs.3.85 Lakh")
    dt.Rows.Add(12, 8, "Rs.4.34 Lakh")
    dt.Rows.Add(13, 9, "Rs.5.44 Lakh")
    dt.Rows.Add(14, 2, "Avenger Cruise")
    dt.Rows.Add(15, 14, "Rs.1.03 Lakh")
    Return dt
End Function

Screenshot