Multiple ValueMembers

4 posts, 1 answers
  1. Ryan
    Ryan avatar
    35 posts
    Member since:
    Jul 2015

    Posted 09 Dec 2015 Link to this post

    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!

  2. Ryan
    Ryan avatar
    35 posts
    Member since:
    Jul 2015

    Posted 09 Dec 2015 in reply to Ryan Link to this post

    Those properties don't make any sense... it should say _Value1 = value, _Value2 = value... typo!
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    712 posts

    Posted 10 Dec 2015 Link to this post

    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
  5. Ryan
    Ryan avatar
    35 posts
    Member since:
    Jul 2015

    Posted 18 Dec 2015 in reply to Hristo Merdjanov Link to this post

    Hristo,

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

Back to Top