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

Place to store data in a treeview.

6 Answers 462 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Jeff Bradshaw
Top achievements
Rank 1
Jeff Bradshaw asked on 01 Nov 2010, 09:27 PM
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.

6 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 03 Nov 2010, 12:12 PM
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. 
0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 04 Jan 2011, 07:57 PM
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
0
Richard Slade
Top achievements
Rank 2
answered on 04 Jan 2011, 08:42 PM
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
0
Julian Benkov
Telerik team
answered on 06 Jan 2011, 02:53 PM
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.
0
Accepted
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 07 Jan 2011, 10:46 AM
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

0
Richard Slade
Top achievements
Rank 2
answered on 07 Jan 2011, 10:49 AM
Hi Erwin,

Yes, I agree, this would make the code more readable in many situations.
Thanks
Richard
Tags
Treeview
Asked by
Jeff Bradshaw
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
erwin
Top achievements
Rank 1
Veteran
Iron
Julian Benkov
Telerik team
Share this question
or