We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to load TreeNavigator from DB

I am trying to load the TreeNavigator from a database based on user permissions. The list could be 3-4 child deep with some not having any children. I am trying to achieve this is VB.net.

Thanks, Martin

5 Replies

SR Sabaridass Ramamoorthy Syncfusion Team April 30, 2019 08:50 AM UTC

Hi Martin, 
 
Thanks for contacting Syncfusion Support. 
 
We have prepared a simple sample to achieve your requirement and it can be downloaded from below location. 
 
Kindly refer to the below code example which helps you to add child nodes based on your needs. 
#Form1.cs 
 
private void TreeNavigator1_SelectionChanged(TreeNavigator sender, SelectionStateChangedEventArgs e) 
        { 
            TreenavigatorItemsUpdate(treeNavigator1, e); 
        } 
        private void TreenavigatorItemsUpdate(TreeNavigator sender, SelectionStateChangedEventArgs e) 
        { 
            TreeNavigator tree = sender as TreeNavigator; 
            dataBaseView.Table.Rows.Cast<DataRow>().ToList().ForEach(x => 
               { 
                   if (dataBaseView.Table.Columns.Contains(e.SelectedItem.Text)) 
                       e.SelectedItem.Items.Add(new TreeMenuItem() { Text = x[dataBaseView.Table.Columns[e.SelectedItem.Text]].ToString() }); 
               }); 
            
        } 
 
If the above solution doesn’t meet your actual requirement, you can share some more detailed information about your requirement. So that we could provide the solution at the earliest. 
 
Regards, 
Sabaridass R 
 



MA Martin April 30, 2019 09:40 AM UTC

Hi, Thanks for the example. Am i right in thinking that you only add the child items upon each click of the parent and not load them at the start? Also i am having an issue converting to VB, do you have any samples in VB?

Thanks, Martin


SR Sabaridass Ramamoorthy Syncfusion Team April 30, 2019 10:41 AM UTC

Hi Martin, 
 
You can also add child nodes at the beginning while adding parent nodes. Kindly refer to the below code example. 
Private Sub FillItems(ByVal tree As TreeNavigator) 
                For Each col As DataColumn In dataBaseView.Table.Columns 
                           Me.treeNavigator1.Items.Add(New TreeMenuItem() With {.Text = col.ColumnName}) 
                Next col 
                     For Each x As DataRow In dataBaseView.Table.Rows 
                           For Each y As TreeMenuItem In Me.treeNavigator1.Items 
                                  If dataBaseView.Table.Columns.Contains(y.Text) Then 
                                         y.Items.Add(New TreeMenuItem() With {.Text = x(dataBaseView.Table.Columns(y.Text)).ToString()}) 
                                  End If 
 
                           Next y 
                     Next x 
              End Sub 
 
We have prepared a sample in VB with modified code changes and it can be downloaded from below location. 
 
Regards, 
Sabaridass R 



MA Martin April 30, 2019 11:57 AM UTC

Thank you, i have successfully got the parent and child node working. But how would i go about adding a child to a child? I need to be able to go 5 childs deep.

Thanks, Martin


JP Jagadeesan Pichaimuthu Syncfusion Team May 1, 2019 12:06 PM UTC

  
Hi Martin,

Thanks for the update.

In TreeNavigator there is no default support to bind items through Database and it can be achieved by loading TreeMenuItem in iteration process from DataTable. We have modified the sample as per your requirement and it can be downloaded from below location.

Sample: TreeNavigator_DataBinding 

In this sample we have initialized the DataTable with required data, integrated the DataTable in DataSet and created and loaded TreeMenuItem in TreeNavigator.
 

Please check the above sample and let us know if you require further assistance on this.

Regards,
 
Jagadeesan

Loader.
Up arrow icon