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

Removing a tree node

1 Answer 186 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Sonya L
Top achievements
Rank 1
Sonya L asked on 17 Jan 2011, 05:43 PM
I have a treeview control bound to a BindingList.  If I call .Remove() on one of the tree nodes, should it delete the underlying item in the BindingList?  I checked the count before and after calling .Remove, and the count on the bindinglist is the same.  I want the item to be deleted, so what do I need to do differently?  Also, I have tried removing the items from the underlying BindingList, and have noticed that the tree nodes are not removed.  I can add nodes to the bindinglist, and they will show up as new tree nodes, but I am having issues with removal.  Thanks for any help!

1 Answer, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 20 Jan 2011, 02:19 PM
Hi Sonya L,

This a known issue in RadTreeView. Currently, we are reimplementing our RadTreeView introducing a new data engine and virtualized UI. I am glad to inform you that this feature will be supported by default including all CRUD operations.

For the time being, you must manually remove the attached item from the bound object and this automatically will remove the RadTreeNode item:

using System;
using System.ComponentModel;
using System.Windows.Forms;
 
namespace LabApp
{
    public partial class Form6 : Form
    {
        BindingList<Person> persons;
 
        public Form6()
        {
            InitializeComponent();
 
            this.persons = new BindingList<Person>();
            this.persons.Add(new Person(1, 0, "Peter"));
            this.persons.Add(new Person(2, 0, "Stefan"));
            this.persons.Add(new Person(3, 1, "John"));
            this.persons.Add(new Person(4, 2, "James"));
            this.persons.Add(new Person(5, 4, "Ivan"));
 
            this.radTreeView1.DataSource = this.persons;
            this.radTreeView1.ValueMember = "Id";
            this.radTreeView1.DisplayMember = "Name";
            this.radTreeView1.ParentIDMember = "ParentId";
 
 
        }
 
        private void radButton1_Click(object sender, EventArgs e)
        {
            if (this.radTreeView1.SelectedNode != null)
            {
                Person person = this.radTreeView1.SelectedNode.DataBoundItem as Person;
                if (person != null)
                {
                    this.persons.Remove(person);
                }
            }
        }
 
        public class Person
        {
            string name;
            int id;
            int parentId;
 
            public Person()
            {
 
            }
 
            public Person(int id, int parentId, string name)
            {
                this.id = id;
                this.parentId = parentId;
                this.name = name;
            }
 
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
 
            public int Id
            {
                get { return id; }
                set { id = value; }
            }
 
            public int ParentId
            {
                get { return parentId; }
                set { parentId = value; }
            }
        }
    }
}

I hope this helps.

All the best,
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.
Tags
Treeview
Asked by
Sonya L
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Share this question
or