Completed
Last Updated: 02 Feb 2018 15:36 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 19 Jul 2016 07:54
Category: TreeView
Type: Bug Report
1
FIX. RadTreeView - deleted nodes via the context menu are not removed from the DataSource
To reproduce:

DataTable dt = new DataTable();
 
public Form1()
{
    InitializeComponent();

    dt.Columns.Add("Id", typeof(int));
    dt.Columns.Add("ParentID", typeof(int));
    dt.Columns.Add("NodeName", typeof(string));
    dt.Rows.Add(1, 0, "Category1");
    dt.Rows.Add(2, 1, "Category2");
    dt.Rows.Add(3, 0, "Category3");
    dt.Rows.Add(4, 3, "Category4");
    dt.Rows.Add(5, 3, "Category5");
    dt.Rows.Add(6, 3, "Category6");
    dt.Rows.Add(7, 6, "Category7");
    dt.Rows.Add(8, 6, "Category8"); 

    this.radTreeView1.DataSource = dt; 

    this.radTreeView1.DisplayMember = "NodeName";
    this.radTreeView1.ChildMember = "ID";
    this.radTreeView1.ParentMember = "ParentID";

    this.radTreeView1.ExpandAll();

    this.radTreeView1.AllowAdd = true;
    this.radTreeView1.AllowRemove = true;
    this.radTreeView1.AllowDefaultContextMenu = true;

    this.radLabel1.Text = "Number of rows: " + dt.Rows.Count;
    
    this.radTreeView1.NodeRemoved += radTreeView1_NodeRemoved;
}

private void radTreeView1_NodeRemoved(object sender, Telerik.WinControls.UI.RadTreeViewEventArgs e)
{
    this.radLabel1.Text = "Number of rows: " + dt.Rows.Count;
}

Workaround: perform the delete operation programmatically by modifying the default context menu:

 this.radTreeView1.ContextMenuOpening += radTreeView1_ContextMenuOpening;

private void radTreeView1_ContextMenuOpening(object sender, TreeViewContextMenuOpeningEventArgs e)
{
    foreach (RadMenuItem item in e.Menu.Items)
    {
        if (item.Text == "&Delete")
        {
            item.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
        }
        else
        {
            item.Visibility = Telerik.WinControls.ElementVisibility.Visible;
        }
    }

    RadMenuItem removeItem = new RadMenuItem("Remove");
    e.Menu.Items.Add(removeItem);
    removeItem.Click += item_Click;
}

private void item_Click(object sender, EventArgs e)
{
    DeleteSubNodes(this.radTreeView1.SelectedNode);
    this.radLabel1.Text = "Number of rows: " + dt.Rows.Count;
}

private void DeleteSubNodes(Telerik.WinControls.UI.RadTreeNode node)
{
    if (node.Nodes.Count == 0)
    {
        dt.Rows.Remove(((DataRowView)node.DataBoundItem).Row);
    }
    else
    {
        foreach (RadTreeNode n in node.Nodes)
        {
            DeleteSubNodes(n);
        }
        dt.Rows.Remove(((DataRowView)node.DataBoundItem).Row);
    }
}
Attached Files:
0 comments