Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate a treeview from a list of objects

I'm having a problem populating my treeview from my list of objects. I've been looking for solutions on google, I found some topic close to my problem, but none of them solved it.

I have a List with properties for each object : Name and Group.

I would like to populate my treeview like below :

+---Group 1
|   |
|   +--------object.Name <-- 
|   +--------object.Name <-- all objects with object.Group = Group 1
|   +--------object.Name <--
|
+---Group 2
|   |
|   +--------object.Name <-- 
|   +--------object.Name <-- all objects with object.Group = Group 2
|   +--------object.Name <--
|

and so on.

Thanks.

like image 279
user2346200 Avatar asked May 03 '13 09:05

user2346200


People also ask

How do I create a folder in TreeView?

Combine(treeView1. SelectedNode. Tag as string, "New Folder")); This will create a directory which is represented by the combination of the path of the current node and the string 'New Folder'.

How does TreeView work?

The TreeView control is used to display hierarchical representations of items similar to the ways the files and folders are displayed in the left pane of the Windows Explorer. Each node may contain one or more child nodes. Let's click on a TreeView control from the Toolbox and place it on the form.


3 Answers

Worked very well, thanks. I've just added a few lines at the beginning and at the end of the else as below.

private void PopulateTreeView()
    {
        ListOfObjectsSorted = ListOfObjects.OrderBy(r => r.Group).ToList();
        var topNode = new TreeNode("Select all");
        treeView1.Nodes.Add(topNode);
        string currentGroup = ListOfObjectsSorted.First().Group;
        var treeNodes = new List<TreeNode>();
        var childNodes = new List<TreeNode>();
        foreach (Object obj in ListOfObjectsSorted )
        {
            if (currentGroup == rule.Group)
                childNodes.Add(new TreeNode(obj.Name));
            else
            {
                if (childNodes.Count > 0)
                {
                    treeNodes.Add(new TreeNode(currentGroup, childNodes.ToArray()));
                    childNodes = new List<TreeNode>();
                }
                childNodes.Add(new TreeNode(obj.Name));
                currentGroup = obj.Group;
            }
        }
        if (childNodes.Count > 0)
        {
            treeNodes.Add(new TreeNode(currentGroup, childNodes.ToArray()));
        }
        treeView1.Nodes[0].Nodes.AddRange(treeNodes.ToArray());
    }
like image 181
user2346200 Avatar answered Oct 01 '22 10:10

user2346200


Is your problem technical, or just how to approach it? You could sort by object.Group, and then create a top level node every time the group changes.

Edit: Here's some sample code below, I have not tested it so treat it more as a guideline:

string currentGroup = null;
List<TreeNode> treeNodes = new List<TreeNode>();
List<TreeNode> childNodes = new List<TreeNode>();
foreach (BusinessObject obj in objectList)
{
    if (currentGroup == obj.Group)
        childNodes.Add(new TreeNode(obj.Name));
    else
    {
        if (childNodes.Count > 0)
        {
            treeNodes.Add(new TreeNode(currentGroup, childNodes.ToArray()));
            childNodes = new List<TreeNode>();
        }
        currentGroup = obj.Group;
    }
}
if (childNodes.Count > 0)
{
    treeNodes.Add(new TreeNode(currentGroup, childNodes.ToArray()));
}
treeView.Nodes.AddRange(treeNodes.ToArray());
like image 30
David Cummins Avatar answered Oct 01 '22 09:10

David Cummins


Here some in VB...

Try

        Me.Cursor = Cursors.WaitCursor
        If rtb.Visible Then line = rtb.Lines
        Dim i, j As Integer
        Dim myLst As New ListBox
        Dim nod0 As New TreeNode
        Dim nod1 As New TreeNode
        Dim nodGet As New TreeNode
        Dim nodSet As New TreeNode
        'Aktualna pozicia
        If trw.SelectedNode Is Nothing Then
        Else
            nodGet = trw.SelectedNode
        End If

        For Each nod0 In trw.Nodes
            If nod0.IsExpanded Then
                myLst.Items.Add(nod0.Tag)
                For Each nod1 In nod0.Nodes
                    If nod1.IsExpanded Then myLst.Items.Add(nod1.Tag)
                Next
            End If
        Next

        Dim cntr() As Integer = {-1, -1, -1}

        trw.Nodes.Clear()
        trw.ShowPlusMinus = False
        trw.ShowRootLines = False
        trw.CheckBoxes = False
        For i = 0 To UBound(line)
            If Mid(line(i), 1, 1) = "\" Then
                j = line(i).IndexOf(dod)
                If j > 0 Then
                    s = Mid(line(i), 1, j)
                Else
                    s = line(i)
                End If
                If Mid(s, 1, 4) = "\\\\" Then
                    trw.Nodes.Add(Mid(s, 5))
                    cntr(0) = cntr(0) + 1
                    cntr(1) = -1
                    cntr(2) = -1
                    trw.Nodes(cntr(0)).Tag = s
                    trw.Nodes(cntr(0)).ImageIndex = 4
                    trw.Nodes(cntr(0)).SelectedImageIndex = 5
                    If trw.Nodes(cntr(0)).Tag = nodGet.Tag Then nodSet = trw.Nodes(cntr(0))
                Else
                    If Mid(s, 1, 3) = "\\\" Then
                        If cntr(0) = -1 Then trw.Nodes.Add("...") : cntr(0) = 0 : cntr(1) = -1
                        trw.Nodes(cntr(0)).Nodes.Add(Mid(s, 4))
                        cntr(1) = cntr(1) + 1
                        cntr(2) = -1
                        trw.Nodes(cntr(0)).Nodes(cntr(1)).Tag = trw.Nodes(cntr(0)).Tag.ToString & eol & s
                        trw.Nodes(cntr(0)).Nodes(cntr(1)).ImageIndex = 1
                        trw.Nodes(cntr(0)).Nodes(cntr(1)).SelectedImageIndex = 2
                        If trw.Nodes(cntr(0)).Nodes(cntr(1)).Tag = nodGet.Tag Then nodSet = trw.Nodes(cntr(0)).Nodes(cntr(1))
                    Else
                        If Mid(s, 1, 2) = "\\" Then
                            If cntr(0) = -1 Then trw.Nodes.Add("...") : cntr(0) = 0 : cntr(1) = -1
                            If cntr(1) = -1 Then trw.Nodes(cntr(0)).Nodes.Add("...") : cntr(1) = 0 : cntr(2) = -1
                            trw.Nodes(cntr(0)).Nodes(cntr(1)).Nodes.Add(Mid(s, 3))
                            cntr(2) = cntr(2) + 1
                            trw.Nodes(cntr(0)).Nodes(cntr(1)).Nodes(cntr(2)).Tag = trw.Nodes(cntr(0)).Nodes(cntr(1)).Tag.ToString & eol & s
                            trw.Nodes(cntr(0)).Nodes(cntr(1)).Nodes(cntr(2)).ImageIndex = 3
                            trw.Nodes(cntr(0)).Nodes(cntr(1)).Nodes(cntr(2)).SelectedImageIndex = 4
                            If trw.Nodes(cntr(0)).Nodes(cntr(1)).Nodes(cntr(2)).Tag = nodSet.Tag Then nodSet = trw.Nodes(cntr(0)).Nodes(cntr(1)).Nodes(cntr(2))
                        End If
                    End If
                End If
            End If
        Next i
        'Navrat na aktualnu poziciu
        Application.DoEvents()
        For i = 0 To myLst.Items.Count - 1
            For Each nod0 In trw.Nodes
                If nod0.Tag = myLst.Items(i).ToString Then nod0.Expand()
                For Each nod1 In nod0.Nodes
                    If nod1.Tag = myLst.Items(i).ToString Then nod1.Expand()
                Next
            Next
        Next i
        Application.DoEvents()
        If nodSet.Tag <> "" Then trw.SelectedNode = nodSet
    Catch ex As Exception
        MsgBox(ex.ToString, MsgBoxStyle.Exclamation)
    Finally
        Me.Cursor = Cursors.Default
    End Try
like image 28
tibi Avatar answered Oct 01 '22 09:10

tibi