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

Get All Value Node When Use Node Formatting

2 Answers 106 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Mehdi
Top achievements
Rank 1
Mehdi asked on 25 Sep 2013, 06:33 PM
Hi
I want Change Node ForeColor In Node Formatting By Check Node Value. But I Get Value For Root Node Only.
Let Me Know How Can I Get All Node Value In Node Formatting PLS

TNXXXXXXXXXXXXX

private void RadTreeView1NodeFormatting(object sender, TreeNodeFormattingEventArgs e)
        {
            try
            {
                if (BSGroupRoleAccess.Cast<RoleAccess>().Any(x=>x.ID.ToString()==e.Node.Value.ToString()))
                {
                    ........
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

2 Answers, 1 is accepted

Sort by
0
Mehdi
Top achievements
Rank 1
answered on 26 Sep 2013, 06:28 AM
When I Set "Parrent Member , Child Member and DataSource" In Code (OnLoadEvent) Everything Is Fine, But When I Use TreeView Property Builder I Get Root Value Only 
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Sep 2013, 11:19 AM
Hello Mehdi,

Thank you for contacting Telerik Support.

When you populate RadTreeNode with data at design-time, its nodes have only Text, but no Value is available (please, see the attached picture DesignTimeNodeTextValue.png). When you populate with data at run-time using DataSource property, RadTreeView nodes are initialized with both Text and Value. Please, see the following code snippet, demonstrating how to assign value of each node if you populate the tree view at design-time:
public Form1()
{
    InitializeComponent();
 
    //RadTreeView populated at design-time
    foreach (RadTreeNode node in radTreeView1.Nodes)
    {
        InitializeValue(node);              
    }
 
    radTreeView1.NodeFormatting += radTreeView1_NodeFormatting;
 
    //RadTreeView populated at run-time
    List<Product> products = new List<Product>();
    products.Add(new Product(567, "Bicycle", 5));
    products.Add(new Product(456, "Car", 5000));
    products.Add(new Product(789, "Bike", 1500));
    List<Category> categories = new List<Category>();
    categories.Add(new Category("Bikes", products));
    categories.Add(new Category("Accessories", null));
    categories.Add(new Category("Clothing", null));
    radTreeView2.DataSource = categories;
    radTreeView2.DisplayMember = "Name\\Description";
    radTreeView2.ChildMember = "Categories\\Products";   
 
    radTreeView2.NodeFormatting += radTreeView2_NodeFormatting;
}
 
private void InitializeValue(RadTreeNode node)
{
    node.Value = node.Text;
    if (node.Nodes.Count > 0)
    {
        foreach (RadTreeNode childNode in node.Nodes)
        {
            InitializeValue(childNode);              
        }
    }
}
 
private void radTreeView1_NodeFormatting(object sender, TreeNodeFormattingEventArgs e)
{
    if (e.Node.Value != null && e.Node.Value.ToString().Contains("1"))
    {
        e.Node.ForeColor = Color.Fuchsia;
    }
}
 
private void radTreeView2_NodeFormatting(object sender, Telerik.WinControls.UI.TreeNodeFormattingEventArgs e)
{
    if (e.Node.Value.ToString().Contains("e"))
    {
        e.Node.ForeColor = Color.Red;
    }
}
 
public class Product
{
    private int _id;
    private string _description;
    private float _price;
 
    public int ID
    {
        get
        {
            return _id;
        }
        set
        {
            _id = value;
        }
    }
 
    public string Description
    {
        get
        {
            return _description;
        }
        set
        {
            _description = value;
        }
    }
 
    public float Price
    {
        get
        {
            return _price;
        }
        set
        {
            _price = value;
        }
    }
 
    public Product(int id, string description, float price)
    {
        _id = id;
        _description = description;
        _price = price;
    }
}
 
public class Category
{
    public Category(string name, List<Product> products)
    {
        _name = name;
        _products = products;
    }
 
    private List<Product> _products;
    private string _name;
 
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }
 
    public List<Product> Products
    {
        get
        {
            return _products;
        }
        set
        {
            _products = value;
        }
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
Treeview
Asked by
Mehdi
Top achievements
Rank 1
Answers by
Mehdi
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or