Baldik
Baldik

Reputation: 117

Devexpress Treelist adding objects only show object name

I'm trying to add objects to the treelist of devexpress. But sadly it does not fill the cells automatically, instead it displays in every cell the object name.

Public Class TempItem

Private m_name As String
Private m_value As String


Public Property Name() As String
    Get
        Return m_name
    End Get
    Set(value As String)
        m_name = value
    End Set
End Property
Public Property Value() As String
    Get
        Return m_value
    End Get
    Set(value As String)
        m_value = value
    End Set
End Property

End Class

Private Function TempItem1() As List(Of TempItem)
    Dim tmpList As New List(Of TempItem)

    tmpList.Add(New TempItem("Feature", "0"))
    tmpList.Add(New TempItem("Feature2", "1"))


    Return tmpList
End Function

And I'm adding the objects by this line:

tlEditor.Nodes.Add(TempItem1.ToArray)

I have two columns in my treelist. For the first column I've set the FieldName to Name and the second column I've set the fieldname to Value expecting the treelist to show the value of the properties in the TempItem class. In this case the treelist should show:

Feature | 0 Feature2 | 1

But instead it shows WindowsApplication1.TempItem in each cell. Like

WindowsApplication1.TempItem | WindowsApplication1.TempItem WindowsApplication1.TempItem | WindowsApplication1.TempItem

What have I done wrong?

Upvotes: 2

Views: 169

Answers (1)

dcreight
dcreight

Reputation: 641

Your list is holding new instances of the Class TempItem. I would use an array of strings instead for this. You can modify your class for this or just set up a list as the example below (C#):

    public List<string[]> tmplist = new List<string[]>();
            string[] str1 = new string[] { "test1", "1" };
            string[] str2 = new string[] { "test2", "2" };
            tmplist.Add(str1);
            tmplist.Add(str2);

Then create a function that takes the list and populates a TreeNodeCollection with the values in the list:

    public void CreateList(List<string[]> ars, TreeView tv)
    {
        foreach (var array in ars)
        {
            AddItems(array, 0, tv.Nodes);
        }
    }

    void AddItems(string[] array, int index, TreeNodeCollection nodes)
    {
        if (index < array.Length)
        {
            var nextNode = AddValue(array[index], nodes);
            AddItems(array, index + 1, nextNode.Nodes);
        }
    }

    TreeNode AddValue(string value, TreeNodeCollection nodes)
    {
        var index = nodes.IndexOfKey(value);
        if (index == -1)
        {
            var newNode = new TreeNode(value) { Name = value };
            nodes.Add(newNode);
            return newNode;
        }
        return nodes[index];
    }

Then simply call CreateList with your parameters:

CreateList(tmplist, treeView1);

Upvotes: 1

Related Questions