how to expand to a given child

Hi,

I'm using a treeview(AcccountsTree) configured to display a hierarchy of
    public class AccountModel
    {
        public int AccountID { get; set; }
        public string sAccountID { get; set; }
        public Int64 AccountNo { get; set; }
        public int ParentID { get; set; }
        public string sParentID { get; set; }
        public string Name { get; set; }
        public string NameNo { get; set; }
        public string Details { get; set; }
        public decimal InitBalance { get; set; }
        public int Level { get; set; }
        public bool Analytic { get; set; }
        public int CreatedBy { get; set; }
        public DateTime CreateDate { get; set; }
    }
the datasource is bind to a List<AccountModel> through the method EnumAccountList
            AccountsTree.SelfRelationRootValue = "";
            AccountsTree.ParentMember = "sParentID";
            AccountsTree.ChildMember = "sAccountID";
            AccountsTree.DisplayMember = "NameNo";
            AccountsTree.ValueMember = "AccountID";
            AccountsTree.DataSource = DataModel.EnumAccountList().ToDataTable();

I've a dedicated form to add/update the model properties based on selected node(afterselect event)

my problem is after add/update, to refresh the datasource/treeview :  i'm using 
                        AccountsTree.DataSource = DataModel.EnumAccountList().ToDataTable();                       
                        TreeNodeAdv pNode = AccountsTree.ActiveNode.Parent;
                        while(pNode != null)
                        {
                            pNode.Expand();
                            pNode.Expanded = true;
                            pNode = pNode.Parent;
                        }
                        AccountsTree.Refresh();

while the datasource/treeview is refreshed, but that code fails to open all parent nodes and make the previously selected child visible/selected
your help is highly appreciated

1 Reply 1 reply marked as answer

BR Backia Raj Kanagaraj Syncfusion Team August 18, 2020 11:41 AM UTC

Hi Mohamed, 

Thanks for contacting our Syncfusion support. 

Please find the details for your queries. 

Queries 
Solution 
1. To open all parent nodes using below code. But, it’s not working. 

TreeNodeAdv pNode = AccountsTree.ActiveNode.Parent; 
while(pNode != null) 
   pNode.Expand(); 
   pNode.Expanded = true; 
   pNode = pNode.Parent; 
AccountsTree.Refresh(); 
From your update, we could understood that you want to expand all parent node . You can achieve it using ExpandAll() method. 

Refer below code. 
private void TreeViewAdv1_AfterSelect(object sender, EventArgs e) 
   if (treeViewAdv1.ActiveNode != null) 
   { 
       TreeNodeAdv root = treeViewAdv1.ActiveNode.Parent; 
       while (root != null) 
       { 
            root.ExpandAll(); 
            root.Expanded = true; 
            root = root.Parent; 
       } 
                treeViewAdv1.Refresh(); 
    } 
2. make the previously selected child visible/selected node 
If you want to show the previously selected node, you need to use BringToView() method. Refer the below link. 

Code
private void TreeViewAdv1_AfterSelect(object sender, EventArgs e) 
   if (treeViewAdv1.ActiveNode != null) 
   { 
       TreeNodeAdv root = treeViewAdv1.ActiveNode.Parent; 
       root.BringIntoView(); 
       while (root != null) 
       { 
            root.ExpandAll(); 
            root.Expanded = true; 
            root = root.Parent; 
       } 
                treeViewAdv1.Refresh(); 
    } 


We also prepared the sample for this. Refer below sample. 


Can you please check with the above sample and let us know whether our understanding on your requirement is same? If not, please brief your requirement exactly along with screenshot or video and also by modifying our sample. It will be helpful for us to analyze and provide the solution at the earliest. 

Regards, 
Backia Raj Kanagaraj

Marked as answer
Loader.
Up arrow icon