RadTreeView duplicates items on expand

2 Answers 95 Views
TreeView
Johnny
Top achievements
Rank 3
Bronze
Iron
Iron
Johnny asked on 25 Aug 2022, 01:34 PM

Hello!

I'm having a problem using the RadTreeView. I've got a menu divided into a global parent > parent > child.

This means that I've got 3 levels in my treeview. The problem is once I click on the expand button, if it was the first time then there are no issues however if it is the second time or more, the items start to duplicate with each time.

You may watch the recorded video in order to understand what I meant exactly.

1. How to solve the issue with the duplicates? This did not use to happen on the TreeView

2. Is there a way to actually reduce the time it's taking to load the items? As you can see in the video on how it is slow with every click to expand.

Thanks for the help!


Private Sub creaTV(ByVal user As String)
        Dim acdo As New SiteWeb.AccesDonnees
        Dim rdro As SqlDataReader

        RTV_menuUtilisateur.Nodes.Clear()

            rdro = acdo.RetournerDataReader("SELECT Identifiant, Libelle FROM lobdjetBDD " _
                            & "WHERE Parent is null AND NomUser='" & Trim(user) & "' ORDER BY Libelle,Parent", "EU_BREST")
         
            Do While rdro.Read
                Dim NewNode As New RadTreeNode(rdro(1), rdro(0))
                NewNode.ExpandMode = TreeNodeExpandMode.ServerSide
                NewNode.Expanded = False
                RTV_menuUtilisateur.Nodes.Add(NewNode)
            Loop
            rdro.Close()
            acdo.Dispose()
End Sub

Protected Sub RTV_menuUtilisateur_NodeExpand(sender As Object, e As RadTreeNodeEventArgs) Handles RTV_menuUtilisateur.NodeExpand
        Dim acdo As New SiteWeb.AccesDonnees
        Dim rdro As SqlDataReader
        Dim user As String
        user = DDL_utilisateur_menu.SelectedValue 'this is a combobox with a list of users, ignore it in your tests

        rdro = acdo.RetournerDataReader("SELECT Identifiant, Libelle, LienPage FROM lobdjetBDD WHERE Parent = " _
                                                    & e.Node.Value & " AND NomUser='" & Trim(user) & "' ORDER BY Libelle,Parent", "EU_BREST")
            
            Do While rdro.Read
                Dim NewNode As New RadTreeNode(rdro(1), rdro(0))

                    NewNode.ExpandMode = TreeNodeExpandMode.ServerSide
                    e.Node.Nodes.Add(NewNode)
            Loop
            rdro.Close()
            acdo.Dispose()
    End Sub

2 Answers, 1 is accepted

Sort by
1
Accepted
Johnny
Top achievements
Rank 3
Bronze
Iron
Iron
answered on 29 Aug 2022, 01:04 PM | edited on 29 Aug 2022, 01:18 PM

I have done the following:

1. Create a RadTreeView using the function CreaTV

Private Sub creaTV(ByVal user As String)
        Dim acdo As New SiteWeb.AccesDonnees
        Dim rdro As SqlDataReader

        RTV_menuUtilisateur.Nodes.Clear()

        Try
            'The function RetournerDataReader returns a datareader. The functions code can be found at the bottom
            rdro = acdo.RetournerDataReader(sqlCmdGoesHere, DataBaseName)
            Do While rdro.Read
                Dim NewNode As New RadTreeNode(rdro(1), rdro(0))
                NewNode.ExpandMode = TreeNodeExpandMode.ServerSide
                NewNode.Expanded = False
                RTV_menuUtilisateur.Nodes.Add(NewNode)
            Loop
            rdro.Close()
            acdo.Dispose()
        Catch ex As Exception
           'Handle Exception
        End Try
End Sub

RetournerDataReader Function

Public Function RetournerDataReader(ByVal strSQL As String, ByVal DataBaseName As String) As SqlDataReader
            Dim dr As SqlDataReader

            Try
                OuvrirConnexion(DataBaseName)
                Dim cmd As New SqlCommand(strSQL, m_Connexion)
                cmd.CommandTimeout = 300000
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                cmd.ResetCommandTimeout()
                Return dr

            Catch ex As Exception
                'Handle Exception
            End Try
        End Function

The OuvrirConnexion functions above consist of opening a connection with your database.

 

2. Handle the NodeExpand event of the RadTreeView

ProtectedSub RTV_menuUtilisateur_NodeExpand(sender AsObject, e As RadTreeNodeEventArgs) Handles RTV_menuUtilisateur.NodeExpand Dim acdu AsNew SiteWeb.AccesDonnees Dim rdru As SqlDataAdapter
  Dim acdo AsNew SiteWeb.AccesDonnees Dim rdro As SqlDataReader Dim dt AsNew DataTable() Dim user As String
'Select a user from the combobox in order to display a list of permissions he's got, in the radtreeview user = DDL_utilisateur_menu.SelectedValue Try'This sql counts how many children nodes the current node has
Dim sqlcmd As String = "SELECT lobdjetBDD.Identifiant, lobdjetBDD.Libelle, lobdjetBDD.Parent, lobdjetBDD.ParentGlobal, COUNT(lobdjetBDD.Libelle) as NbChilds " _ & " FROM lobdjetBDD " _ & " LEFT JOIN lobdjetBDD As Children ON lobdjetBDD.Identifiant = Children.Parent " _ & " WHERE Children.Parent = '" & e.Node.Value & "' AND lobdjetBDD.NomUser = '" & Trim(user) & "' " _ & " GROUP BY lobdjetBDD.Identifiant, lobdjetBDD.Libelle, lobdjetBDD.Parent, lobdjetBDD.ParentGlobal"

'sqlcmd2 is a sql query that gets the names of the nodes where parent = e.Node.Value
rdro = acdo.RetournerDataReader(sqlcmd2, DataBaseName)

rdru = acdu.RetournerDataAdapter(sqlcmd, DataBaseName) rdru.Fill(dt) Do While rdro.Read() ForEach row As DataRow In dt.Rows Dim node As New RadTreeNode(rdro(1), rdro(0)) If Convert.ToInt32(row("NbChilds")) > 0Then node.ExpandMode = TreeNodeExpandMode.ServerSide EndIf e.Node.Nodes.Add(node) Next e.Node.Expanded = True e.Node.ExpandMode = TreeNodeExpandMode.ClientSide Loop rdru.Close() acdu.Dispose() Catch ex As Exception 'Handle Exception

End Try

End Sub

The RetournerDataAdapter function used above is simply a function that returns a dataAdapter.

 

This is basically it.

0
Johnny
Top achievements
Rank 3
Bronze
Iron
Iron
answered on 29 Aug 2022, 09:59 AM

The issue was fixed following this Thread.

Thanks anyways.

 

Rumen
Telerik team
commented on 29 Aug 2022, 12:42 PM

I am glad that you've managed to fix the issue! Can you share your exact solution with the community?

Do you think we need to improve the article or make the information more visible/discoverable?

Johnny
Top achievements
Rank 3
Bronze
Iron
Iron
commented on 29 Aug 2022, 12:50 PM

Sure thing, I will submit an answer with my code right after this comment!

I believe the only thing that was problematic is that I did not find that article immediately after searching for a solution. Before finding that article, I had done my own method, which caused the treeview to behave a little bit slower when loading the nodes. This problem got fixed after following the method in the article, which mainly consists on filling and then looping a dataTable and then switching between server-side and client-side depending on the nodes.

The information in the article is quite clear and understandable, in my opinion. A user has to apply the method explained in the example and his problem should be solved.

Tags
TreeView
Asked by
Johnny
Top achievements
Rank 3
Bronze
Iron
Iron
Answers by
Johnny
Top achievements
Rank 3
Bronze
Iron
Iron
Share this question
or