Hi,

In my DB I have 2 tables Units and Groups

In Units I have
Id
Name

In Groups I have
Id
Name
UnitID


This is my code

BMDataClassesDataContext db = new BMDataClassesDataContext();
 TreeNode rootNode = new TreeNode("BM");

            var n1 = from node in db.Units select node;


            foreach (var item in n1)
            {
                var child = new TreeNode(item.Name, item.Id.ToString());
                rootNode.ChildNodes.Add(child);
            }

            TreeView1.Nodes.Add(rootNode);

Now how can I add the table Group so it appears as a child node for the Unit Table?

Thnx beforehand,
batool

Dani AI

Generated

Good start, — your loop creates the Unit nodes correctly. To make each Unit show its Groups as children, fetch Units together with their Groups (one query) and then add a child node for each Group. shows how to add children programmatically and points toward recursive patterns; for this two-level case a grouped join is simpler and avoids an N+1 query problem.

Example pattern (LINQ + ASP.NET TreeView):

// get units and their groups in one query
var q = from u in db.Units
        join g in db.Groups on u.Id equals g.UnitID into groups
        orderby u.Name
        select new { Unit = u, Groups = groups };

TreeView1.Nodes.Clear();
var root = new TreeNode("BM");

foreach (var item in q)
{
    var unitNode = new TreeNode(item.Unit.Name, item.Unit.Id.ToString());
    foreach (var grp in item.Groups.OrderBy(x => x.Name))
    {
        unitNode.ChildNodes.Add(new TreeNode(grp.Name, grp.Id.ToString()));
    }
    root.ChildNodes.Add(unitNode);
}

TreeView1.Nodes.Add(root);

Troubleshooting and tips:

  • Populate once (wrap in if (!IsPostBack) on Page_Load) so nodes are not duplicated.
  • If using LINQ-to-SQL with a Unit->Groups association, eager-load the relationship or use the group-join above to avoid repeated DB hits.
  • Ensure UnitID types match and that the FK mapping exists; empty Groups will simply produce leaf Unit nodes.
  • For large data sets consider lazy loading, paging, or loading only expanded branches. For more than two levels use a small recursive routine that follows parent/child IDs.

Recommended Answers

All 2 Replies

Just copy & run the code..But its in VB.net..U want this??

Option Strict On
Option Explicit On
Option Compare Text

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim lCount As Integer
            Dim tvw As TreeNode

            For lCount = 1 To 5
                tvw = TreeView1.Nodes.Add(lCount.ToString)
                tvw.Name = lCount.ToString
                Application.DoEvents()
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim findnode As TreeNode

            findnode = TreeView1.Nodes.Item("1")
            findnode.Nodes.Add("First")

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try

            Dim findnode As TreeNode

            findnode = TreeView1.Nodes.Item("2")
            findnode.Nodes.Add("Second")

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

Trythis..

hope this helps..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.