This is a migrated thread and some comments may be shown as answers.

Multiple ValueMembers

3 Answers 167 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 09 Dec 2015, 07:57 PM

Hello,

I have a bound RadTreeView that has its datasource set to a datatable. I've set the DisplayMember and ValueMember of the RadTreeView, but now I would like to bind some additional fields to the tree.

I understand that I can create a new class for the RadTreeNode (or just use the Tag property if I only needed one new value) and give it some additional properties, but how can I bind these new properties in the same manner that setting the ValueMember does? My class currently looks like this:

Public Class MyRadTreeNode
    Inherits RadTreeNode
 
    Private _Value1 As String
    Private _Value2 As String
 
    Public Property Value1 As String
        Get
            Return _Value1
        End Get
        Set(value As String)
            _Value1 = Value1
        End Set
    End Property
 
    Public Property Value2 As String
        Get
            Return _Value2
        End Get
        Set(value As String)
            _Value2 = Value2
        End Set
    End Property
End Class

 

If this is not possible, could anyone recommend a workaround? I was looking at just looping through the tree's datasource, but since neither the DisplayMember nor the ValueMember are set to primary key columns, I don't believe this would be possible. I could change the ValueMember to a primary key but I'd like to avoid doing that if at all possible (I'm assuming I would take a pretty big performance hit if I did this).

Thanks in advance!

3 Answers, 1 is accepted

Sort by
0
Ryan
Top achievements
Rank 1
answered on 09 Dec 2015, 10:12 PM
Those properties don't make any sense... it should say _Value1 = value, _Value2 = value... typo!
0
Accepted
Hristo
Telerik team
answered on 10 Dec 2015, 04:13 PM
Hello Ryan,

Thank you for writing.

Indeed, storing this information in the Tag property of the node is a possible solution. Alternatively, you can also achieve the same result by using a custom RadTreeNode, having a property of your choice. Depending on your scenario you can set the value of this field from the data table or from a different source if needed. Please check my code snippet below: 
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
 
        this.radTreeView1.CreateNode += radTreeView1_CreateNode;
        this.radTreeView1.NodeDataBound += radTreeView1_NodeDataBound;
 
        this.radTreeView1.DataSource = GetTable();
        this.radTreeView1.DisplayMember = "Name";
        this.radTreeView1.ParentMember = "ParentId";
        this.radTreeView1.ChildMember = "Id";
        this.radTreeView1.ValueMember = "Id";
 
        this.radTreeView1.ShowLines = true;
        this.radTreeView1.ExpandAll();
    }
 
    private void radTreeView1_CreateNode(object sender, CreateTreeNodeEventArgs e)
    {
        e.Node = new MyRadTreeNode();
    }
 
    private void radTreeView1_NodeDataBound(object sender, RadTreeViewEventArgs e)
    {
        MyRadTreeNode node = (MyRadTreeNode)e.Node;
        node.Description = ((DataRowView)node.DataBoundItem).Row["Description"].ToString();
    }
 
    public DataTable GetTable()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Id", typeof(int));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("ParentId", typeof(int));
        table.Columns.Add("Description", typeof(string));
 
        table.Rows.Add(1, "Parent 1", 0, "Parent Description");
        table.Rows.Add(2, "Parent 2", 0, "Parent Description");
        table.Rows.Add(3, "Parent 3", 0, "Parent Description");
        table.Rows.Add(4, "Parent 4", 0, "Parent Description");
 
        table.Rows.Add(5, "Child 1", 1, "Child Description");
        table.Rows.Add(6, "Child 2", 1, "Child Description");
        table.Rows.Add(7, "Child 3", 3, "Child Description");
        table.Rows.Add(8, "Child 4", 4, "Child Description");
 
        return table;
    }
}
 
public class MyRadTreeNode : RadTreeNode
{
    private string description;
 
    public string Description
    {
        get { return this.description; }
        set { this.description = value; }
    }
}

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Ryan
Top achievements
Rank 1
answered on 19 Dec 2015, 03:18 AM

Hristo,

Thank you - this is exactly what I was looking for!

Tags
Treeview
Asked by
Ryan
Top achievements
Rank 1
Answers by
Ryan
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or