Place to store data in a treeview.

7 posts, 2 answers
  1. Jeff Bradshaw
    Jeff Bradshaw avatar
    27 posts
    Member since:
    Oct 2008

    Posted 01 Nov 2010 Link to this post

    Is the only place to store data in a tree view in the tag field? I need to know info about a node when a person clicks on it. I guess I could make the tag an array of objects and then process them accordingly.

    TIA - Jeff.
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 03 Nov 2010 Link to this post

    Hi, 

    This is a typical area to store a piece of data. We are also using the tag object to store some information, and yes, as it's an object, you can store anything in it. 
  3. UI for WinForms is Visual Studio 2017 Ready
  4. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 04 Jan 2011 Link to this post

    Another option might be to derive your own Node class from RadTreeNode and add whatever additional Properties you need.
    I used to use the Tag Property quite a lot in the past but IMHO deriving your own Node Types leads to cleaner code.
    You have to cast back from SelectedNode to your Class but the remaining code that works with your app specific data is quite typesafe.

    Regards
    Erwin
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 04 Jan 2011 Link to this post

    Hi Erwin,

    That's an interesting way to do it too. May I ask what type of thing you are storing? For most people's needs I'd still probably stick with the tag. It's a fairly obvious place IMO and can of course store anything, though I'd be really interested to know more about what you store in a custm node.
    Thanks
    Richard
  6. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 06 Jan 2011 Link to this post

    Hello Jeff Bradshaw,

    You can use the Tag property to store a reference to your custom object. Another solution is to implement a custom treeview node with your properties and methods. Here is a small example:

    public partial class Form3 : Form
    {
        public class MyNode : RadTreeNode
        {
            private string myStringField;
     
            public string MyStringField
            {
                get { return myStringField; }
                set
                {
                    myStringField = value;
                    this.Text = value;
                }
            }
        }
     
        public Form3()
        {
            InitializeComponent();
        }
     
        private void Form3_Load(object sender, EventArgs e)
        {
            MyNode node = new MyNode();
            node.MyStringField = "Test1";
            this.radTreeView2.Nodes.Add(node);
        }
    }

    I hope this helps.

    Regards,
    Julian Benkov
    the Telerik team
    Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
  7. Answer
    erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 07 Jan 2011 Link to this post

    One of the reasons I sometimes prefer the custom treeNode over the .Tag property is that it lets you encapsulate dependent Properties.

    Using the .Tag, you often end up with code like this:

    RadTreeNode node = new RadTreeNode()
    node.Tag=myObject;
    node.Text=myObject.Text
    node.Value=myObject.ID

    (or more complex depending on how the .Text is related to the object you are representing).

    With a derived class you simply write
    MyTreeNode node = new MyTreeNode(myObject);

    Erwin

  8. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 07 Jan 2011 Link to this post

    Hi Erwin,

    Yes, I agree, this would make the code more readable in many situations.
    Thanks
    Richard
Back to Top
UI for WinForms is Visual Studio 2017 Ready