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

Problem with the selected node to group / ungroup

3 Answers 47 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Cesar
Top achievements
Rank 1
Cesar asked on 12 Mar 2015, 12:56 PM
Hi;

I have a problem when I want a selected node and Group / Ungroup the tree holding the selected node. Here's an example to understand the case:
public Form1()
{
    InitializeComponent();
 
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("ID_PADRE", typeof(int));
    table.Columns.Add("Codigo", typeof(string));
    table.Columns.Add("TEXTO_NODO", typeof(string));
    table.Columns.Add("TIPO", typeof(string));
    table.Rows.Add(1, null, "COD 1", "Descripción 1","C");
    table.Rows.Add(2, null, "COD 2", "Descripción 2","E");
    table.Rows.Add(3, null, "COD 3", "Descripción 3", "A");
    table.Rows.Add(4, null, "COD 4", "Descripción 4", "S");
    table.Rows.Add(5, null, "COD 5", "Descripción 5", "R");
    table.Rows.Add(6, 1, "COD 6", "Descripción 6","S");
    table.Rows.Add(7, 2, "COD 7", "Descripción 7","R");
    table.Rows.Add(8, 3, "COD 8", "Descripción 8","R");
    table.Rows.Add(9, 4, "COD 9", "Descripción 9","S");
    table.Rows.Add(10, 5, "COD 10", "Descripción 10","C");
    table.Rows.Add(11, 1, "COD 11", "Descripción 11","R");
    table.Rows.Add(12, 2, "COD 12", "Descripción 12","S");
    table.Rows.Add(13, 3, "COD 13", "Descripción 13","A");
    table.Rows.Add(14, 4, "COD 14", "Descripción 14","E");
    table.Rows.Add(15, 5, "COD 15", "Descripción 15","R");
    table.Rows.Add(16, 6, "COD 16", "Descripción 16","S");
    table.Rows.Add(17, 7, "COD 17", "Descripción 17","E");
    table.Rows.Add(18, 8, "COD 18", "Descripción 18","R");
    table.Rows.Add(19, 9, "COD 19", "Descripción 19","C");
    table.Rows.Add(20, 10, "COD 20", "Descripción 20","A");
    table.Rows.Add(21, 20, "COD 21", "Descripción 21","S");
     
    this.radTreeView1.DataSource = table;
    this.radTreeView1.DisplayMember = "TEXTO_NODO";
    this.radTreeView1.ChildMember = "ID";
    this.radTreeView1.ParentMember = "ID_PADRE";
    this.radTreeView1.ExpandAll();
 
    this.radGridView1.Relations.AddSelfReference(this.radGridView1.MasterTemplate, "ID_PADRE", "ID");
    this.radGridView1.DataSource = table;
     
}
private void ungroup()
{
    RadTreeNode oNode = this.radTreeView1.SelectedNode;
 
    this.radTreeView1.ChildMember = "";
    this.radTreeView1.ParentMember = "";
 
    this.radTreeView1.SelectedNode = oNode;
}
private void group()
{
    RadTreeNode oNode = this.radTreeView1.SelectedNode;
 
    this.radTreeView1.ChildMember = "ID";
    this.radTreeView1.ParentMember = "ID_PADRE";
    this.radTreeView1.ExpandAll();
 
    this.radTreeView1.SelectedNode = oNode;
}
private void label1_Click(object sender, EventArgs e)
{
    this.ungroup();
}
 
private void label2_Click(object sender, EventArgs e)
{
    this.group();
}

If I have a selected node when calling the method to group / ungroup not maintained after I selected group / ungroup. I tried with what appears in the code but can not.

Thanks for the help.

Regards!

3 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 17 Mar 2015, 08:59 AM
Hi Cesar,

Thank you for writing.

I believe the Filter property of the RadTreeView could deliver the desired behavior. Please refer to this example for detailed information.

I have slightly modified the Group and Ungroup methods in your code, so that we take advantage of the filtering functionality of the RadTreeView. Please see my code snippet below:
private void Ungroup()
{
    this.radTreeView1.Filter = "";
}
 
private void Group()
{
    RadTreeNode oNode = this.radTreeView1.SelectedNode;
    if (oNode != null)
    {
        this.radTreeView1.Filter = oNode.Text;
    }
}

I am also sending you a gif file of the result on my end.

I hope this information helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Cesar
Top achievements
Rank 1
answered on 17 Mar 2015, 10:29 AM
Hi;

Thanks for the awnser. This solution is useful. I do not want to filter, I want all the nodes still appear.

I attach a gif with the current situation. I want that when I grouping / ungroup continue to keep the node that was selected before the action.
0
Hristo
Telerik team
answered on 19 Mar 2015, 02:39 PM
Hi Cesar,

Thank you for writing back.

If I understand you correctly you would like the RadTreeView to display the node which was selected prior to grouping and ungrouping as selected after the operation had been performed. 

If that is the case, you would need to manually find the node which needs to be selected and set the Selected property to true. Using the code you provided last time I prepared a sample implementation how this can be achieved. Please see my code snippet below:
RadTreeNode oNode;
private void Ungroup()
{
    oNode = this.radTreeView1.SelectedNode;
    if (oNode != null)
    {
        this.radTreeView1.ChildMember = "";
        this.radTreeView1.ParentMember = "";
 
        RadTreeNode selectedNode = this.radTreeView1.Nodes.FirstOrDefault(n => n.Name == oNode.Name);
        if (selectedNode != null)
        {
            selectedNode.Selected = true;
        }
    }
}
 
private void Group()
{
    oNode = this.radTreeView1.SelectedNode;
 
    this.radTreeView1.ChildMember = "ID";
    this.radTreeView1.ParentMember = "ID_PADRE";
    this.radTreeView1.ExpandAll();
 
    this.radTreeView1.SelectedNode = oNode;
    this.FindRecursive(this.radTreeView1.Nodes);
}
 
private void FindRecursive(RadTreeNodeCollection nodes)
{
    foreach (RadTreeNode node in nodes)
    {
        if (node.Name == oNode.Name)
        {
            node.Selected = true;
            return;
        }
        else if (node.Nodes.Count > 0)
        {
            FindRecursive(node.Nodes);
        }
    }
}

I am also sending you a gif file with the result on my side.

I hope this information helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Treeview
Asked by
Cesar
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Cesar
Top achievements
Rank 1
Share this question
or