Public Class ClassNodeNavigation
    Public Shared NODE_CONFIGURABLE_ID As Integer
    Public Shared Sub Check_NODE_CONFIG_ID(ENTITY_ID As Integer)
        Try
            If CURRENT_DT_NODE_CONFIGURABLE_NODES.Rows.Count > 0 Then
                'Prüfen ob es für dieses Level einen konfigurierbaren Knoten gibt
                Dim result = (From form In CURRENT_DT_NODE_CONFIGURABLE_NODES.AsEnumerable()
                     Select form
                     Where form.Item("ENTITY_ID") = ENTITY_ID).Single()

                Dim ID = result.Item("GUID")
                If Not IsNothing(ID) Then
                    ClassNodeNavigation.NODE_CONFIGURABLE_ID = ID
                    CURRENT_NODE_CONFIGURABLE_ID = ID
                End If
            End If
        Catch ex As Exception
            ClassNodeNavigation.NODE_CONFIGURABLE_ID = 0
            CURRENT_NODE_CONFIGURABLE_ID = 0
        End Try
       
    End Sub
    Public Shared Function CreateNODE_TAG(Entity As Integer, IDNode As String, Record As Object, NODE_CONFIG_ID As Integer)
        Dim vType, TagString As String
        Try
            If NODE_CONFIGURABLE_ID = NODE_CONFIG_ID Then
                vType = "CONFIG_NODE-" & NODE_CONFIG_ID.ToString
            Else
                vType = "NODE-TYPE-" & NODE_CONFIG_ID.ToString
            End If
            If Not IsDBNull(Record) Then
                TagString = vType & "#" & Entity & "#NODE_ID-" & IDNode & "#RECORD-ID" & Record.ToString
            Else
                TagString = vType & "#" & Entity & "#NODE_ID-" & IDNode
            End If
            Return TagString
        Catch ex As Exception
            ClassLogger.Add(">> Unexpected Error in CreateNODE_TAG (NodeNavigation): " & ex.Message, True)
            MsgBox("Error in CreateNODE_TAG (NodeNavigation):" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            TagString = "ERROR NODETAG_CREATION"
            Return TagString
        End Try
    End Function
    Public Shared Function CreateTreeViewNodesNew1(DT_TREEVIEW_NODES As DataTable, TREEVIEW_IMAGELIST As ImageList)
        Try
            Dim mytrv As New TreeView
            Dim ENTITY_ID As Integer
            Dim expression As String
            expression = "PARENT_GUID = 0"
            Dim MAIN_NODES() As DataRow
            ' Use the Select method to find all rows matching the filter.
            MAIN_NODES = DT_TREEVIEW_NODES.Select(expression, "SEQUENCE, NODE_CAPTION")
            ' For each row create a Node
            For i = 0 To MAIN_NODES.GetUpperBound(0)
                If ENTITY_ID <> MAIN_NODES(i)("ENTITY_ID") Then
                    ENTITY_ID = MAIN_NODES(i)("ENTITY_ID")
                    ClassNodeNavigation.Check_NODE_CONFIG_ID(MAIN_NODES(i)("ENTITY_ID"))
                End If
                Dim NODE_Tag = CreateNODE_TAG(MAIN_NODES(i)("ENTITY_ID"), MAIN_NODES(i)("GUID"), MAIN_NODES(i)("RECORD_ID"), MAIN_NODES(i)("NODE_CONFIG_ID"))
                Dim MAIN_NODE = New TreeNode(MAIN_NODES(i)("NODE_CAPTION")) '"NODE_CAPTION"
                MAIN_NODE.Tag = NODE_Tag
                Dim index As Integer = 0
                For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                    If img = MAIN_NODES(i)("NODE_CONFIG_ID") & "#" & MAIN_NODES(i)("ENTITY_ID") Then
                        Exit For
                    End If
                    index += 1
                Next
                MAIN_NODE.ImageIndex = index
                MAIN_NODE.SelectedImageIndex = index
                CheckSubNodes(DT_TREEVIEW_NODES, MAIN_NODES(i)("GUID"), TREEVIEW_IMAGELIST, MAIN_NODE)
                'Add the node
                mytrv.Nodes.Add(MAIN_NODE)

            Next
            
            Return mytrv
        Catch ex As System.Exception
            ClassLogger.Add(">> Unexpected Error in CreateTreeViewNodesNew (NodeNavigation): " & ex.Message, True)
            MsgBox("Error in CreateTreeViewNodesNew (NodeNavigation):" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function
    Private Shared Function CheckSubNodes(DT_TREEVIEW_NODES As DataTable, PARENT_GUID As Integer, TREEVIEW_IMAGELIST As ImageList, MY_NODE As TreeNode)
        Dim ENTITY_ID As Integer
        Dim expression As String
        expression = "PARENT_GUID = " & PARENT_GUID.ToString
        Dim SUB_NODE_ROW() As DataRow
        ' Use the Select method to find all rows matching the filter.
        SUB_NODE_ROW = DT_TREEVIEW_NODES.Select(expression, "SEQUENCE, NODE_CAPTION")
        ' For each row create a Node
        For i = 0 To SUB_NODE_ROW.GetUpperBound(0)
            If ENTITY_ID <> SUB_NODE_ROW(i)("ENTITY_ID") Then
                ENTITY_ID = SUB_NODE_ROW(i)("ENTITY_ID")
                ClassNodeNavigation.Check_NODE_CONFIG_ID(SUB_NODE_ROW(i)("ENTITY_ID"))
            End If
            Dim NODE_Tag = CreateNODE_TAG(SUB_NODE_ROW(i)("ENTITY_ID"), SUB_NODE_ROW(i)("GUID"), SUB_NODE_ROW(i)("RECORD_ID"), SUB_NODE_ROW(i)("NODE_CONFIG_ID"))
            Dim SUB_NODE = New TreeNode(SUB_NODE_ROW(i)("NODE_CAPTION"))
            SUB_NODE.Tag = NODE_Tag
            Dim index As Integer = 0
            For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                If img = SUB_NODE_ROW(i)("NODE_CONFIG_ID") & "#" & SUB_NODE_ROW(i)("ENTITY_ID") Then
                    Exit For
                End If
                index += 1
            Next
            SUB_NODE.ImageIndex = index
            SUB_NODE.SelectedImageIndex = index
            CheckSubNodes(DT_TREEVIEW_NODES, SUB_NODE_ROW(i)("GUID"), TREEVIEW_IMAGELIST, SUB_NODE)
            'Add the node
            MY_NODE.Nodes.Add(SUB_NODE)
        Next
        Return MY_NODE
    End Function
    Public Shared Function CreateTreeViewNodesNew(DT_TREEVIEW_PER_CONTROLS As DataTable, TREEVIEW_IMAGELIST As ImageList)
        Try
            Dim mytrv As New TreeView
            Dim node As TreeNode
            Dim subNode As TreeNode
            Dim ENTITY_ID As Integer
            For Each row As DataRow In DT_TREEVIEW_PER_CONTROLS.Rows
                If ENTITY_ID <> row.Item("ENTITY_ID") Then
                    ENTITY_ID = row.Item("ENTITY_ID")
                    ClassNodeNavigation.Check_NODE_CONFIG_ID(row.Item("ENTITY_ID"))
                End If
                Dim NODE_Tag = CreateNODE_TAG(row.Item("ENTITY_ID"), row.Item("GUID"), row.Item("RECORD_ID"), row.Item("NODE_CONFIG_ID"))
                'search in the treeview if any country is already present
                node = FindNode(mytrv.Nodes, row.Item("PARENT_GUID"))
                If node IsNot Nothing Then
                    'ParentNode Foun
                    subNode = New TreeNode(row.Item("NODE_CAPTION"))
                    subNode.Tag = NODE_Tag
                    Dim index As Integer = 0
                    For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                        If img = row.Item("NODE_CONFIG_ID") & "#" & row.Item("ENTITY_ID") Then
                            Exit For
                        End If
                        index += 1
                    Next
                    subNode.ImageIndex = index
                    subNode.SelectedImageIndex = index
                    'Add new Node to parentNode
                    node.Nodes.Add(subNode)
                Else
                    Dim NEW_NODE = New TreeNode(row.Item("NODE_CAPTION"))
                    NEW_NODE.Tag = NODE_Tag
                    Dim index As Integer = 0
                    For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                        If img = row.Item("NODE_CONFIG_ID") & "#" & row.Item("ENTITY_ID") Then
                            Exit For
                        End If
                        index += 1
                    Next
                    NEW_NODE.ImageIndex = index
                    NEW_NODE.SelectedImageIndex = index

                    'Add the node
                    'node.Nodes.Add(NEW_NODE)
                    mytrv.Nodes.Add(NEW_NODE)
                End If
            Next
            Return mytrv
        Catch ex As System.Exception
            ClassLogger.Add(">> Unexpected Error in CreateTreeViewNodesNew (NodeNavigation): " & ex.Message, True)
            MsgBox("Error in CreateTreeViewNodesNew (NodeNavigation):" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function
    Private Shared Function FindNode(tncoll As TreeNodeCollection, PARENT_GUID As Integer) As TreeNode
        Try
            Dim tnFound As TreeNode
            For Each tnCurr As TreeNode In tncoll
                If tnCurr.Tag.ToString.Contains("#NODE_ID-" & PARENT_GUID) Then
                    Return tnCurr
                End If
                tnFound = FindNode(tnCurr.Nodes, PARENT_GUID)
                If tnFound IsNot Nothing Then
                    Return tnFound
                End If
            Next
            Return Nothing
        Catch ex As Exception
            ClassLogger.Add(">> Unexpected Error in FindNode (NodeNavigation): " & ex.Message, True)
            MsgBox("Error in FindNode (NodeNavigation):" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
      
    End Function
  
    Public Shared Function CreateTreeViewNodes(DT_TREEVIEW_PER_CONTROLS As DataTable, TREEVIEW_IMAGELIST As ImageList)
        Try
            Dim mytrv As New TreeView
            ' Rootnode erstellen und taggen
            If DT_TREEVIEW_PER_CONTROLS.Rows.Count > 1 Then
                ' Presuming the DataTable has a column named ENTITY_ID and TYPE_NODE.
                Dim expression As String
                expression = "TYPE_NODE = 0"
                Dim foundRowsLevel0() As DataRow
                ' Use the Select method to find all rows matching the filter.
                foundRowsLevel0 = DT_TREEVIEW_PER_CONTROLS.Select(expression, "SEQUENCE,NODE_CAPTION")
                Dim i As Integer
                Dim NODE_CONFIG_ID
                Dim LEVEL0_NODE As TreeNode

                ' For each row create a Node
                For i = 0 To foundRowsLevel0.GetUpperBound(0)
                    Dim ID = foundRowsLevel0(i)("GUID")
                    Dim LevelEntity = foundRowsLevel0(i)("ENTITY_ID")
                    ClassNodeNavigation.Check_NODE_CONFIG_ID(LevelEntity)
                    Dim controlID = foundRowsLevel0(i)("CONTROL_ID")
                    Dim nodetext = foundRowsLevel0(i)("NODE_CAPTION")
                    Dim Type_node = foundRowsLevel0(i)("TYPE_NODE")
                    Dim RECORD_ID = foundRowsLevel0(i)("RECORD_ID")
                    NODE_CONFIG_ID = foundRowsLevel0(i)("NODE_CONFIG_ID")
                    LEVEL0_NODE = New TreeNode(nodetext)
                    LEVEL0_NODE.Tag = CreateNODE_TAG(LevelEntity, ID, RECORD_ID, NODE_CONFIG_ID)

                    Dim index As Integer = 0
                    For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                        If img = NODE_CONFIG_ID & "#" & LevelEntity Then
                            Exit For
                        End If
                        index += 1
                    Next
                    LEVEL0_NODE.ImageIndex = index
                    LEVEL0_NODE.SelectedImageIndex = index

                    '### LEVEL 1 laden #######
                    Dim expressionLevel1 As String
                    expressionLevel1 = "PARENT_GUID = " & ID 'TYPE_NODE = 1 AND  Einschränkung auf Type 2 und Parent_Guid
                    Dim foundRowsLevel1() As DataRow
                    foundRowsLevel1 = DT_TREEVIEW_PER_CONTROLS.Select(expressionLevel1, "SEQUENCE,NODE_CAPTION")
                    Dim j As Integer
                    For j = 0 To foundRowsLevel1.GetUpperBound(0)
                        Dim ID1 = foundRowsLevel1(j)("GUID")
                        Dim LevelEntity1 = foundRowsLevel1(j)("ENTITY_ID")
                        ClassNodeNavigation.Check_NODE_CONFIG_ID(LevelEntity1)
                        Dim controlID1 = foundRowsLevel1(j)("CONTROL_ID")
                        Dim nodetext1 = foundRowsLevel1(j)("NODE_CAPTION")
                        Dim Type_node1 = foundRowsLevel1(j)("TYPE_NODE")
                        RECORD_ID = foundRowsLevel1(j)("RECORD_ID")
                        NODE_CONFIG_ID = foundRowsLevel1(j)("NODE_CONFIG_ID")
                        ' Node erstellen..
                        Dim LEVEL1_NODE As TreeNode
                        LEVEL1_NODE = New TreeNode(nodetext1)

                        LEVEL1_NODE.Tag = CreateNODE_TAG(LevelEntity, ID, RECORD_ID, NODE_CONFIG_ID)

                        index = 0
                        For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                            If img = NODE_CONFIG_ID & "#" & LevelEntity1 Then
                                Exit For
                            End If
                            index += 1
                        Next
                        LEVEL1_NODE.ImageIndex = index
                        LEVEL1_NODE.SelectedImageIndex = index
                        ' ..und einfügen
                        LEVEL0_NODE.Nodes.Add(LEVEL1_NODE)
                        '### LEVEL 2 laden #######
                        Dim expressionLevel2 As String
                        expressionLevel2 = "PARENT_GUID = " & ID1 'TYPE_NODE = 2 AND Einschränkung auf Type  und Parent_Guid
                        Dim foundRowsLevel2() As DataRow
                        foundRowsLevel2 = DT_TREEVIEW_PER_CONTROLS.Select(expressionLevel2, "SEQUENCE,NODE_CAPTION")
                        Dim k As Integer
                        For k = 0 To foundRowsLevel2.GetUpperBound(0)
                            Dim ID2 = foundRowsLevel2(k)("GUID")
                            Dim LevelEntity2 = foundRowsLevel2(k)("ENTITY_ID")
                            ClassNodeNavigation.Check_NODE_CONFIG_ID(LevelEntity2)
                            Dim controlID2 = foundRowsLevel2(k)("CONTROL_ID")
                            Dim nodetext2 = foundRowsLevel2(k)("NODE_CAPTION")
                            RECORD_ID = foundRowsLevel2(k)("RECORD_ID")
                            NODE_CONFIG_ID = foundRowsLevel2(k)("NODE_CONFIG_ID")
                            ' Node erstellen..
                            Dim LEVEL2_NODE As TreeNode
                            LEVEL2_NODE = New TreeNode(nodetext2)
                            LEVEL2_NODE.Tag = CreateNODE_TAG(LevelEntity, ID, RECORD_ID, NODE_CONFIG_ID)

                            index = 0
                            For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                If img = NODE_CONFIG_ID & "#" & LevelEntity2 Then
                                    Exit For
                                End If
                                index += 1
                            Next
                            LEVEL2_NODE.ImageIndex = index
                            LEVEL2_NODE.SelectedImageIndex = index
                            ' ..und einfügen
                            LEVEL1_NODE.Nodes.Add(LEVEL2_NODE)
                            '### LEVEL 3 laden #######
                            Dim expressionLevel3 As String
                            expressionLevel3 = "PARENT_GUID = " & ID2 'TYPE_NODE = 3 AND  Einschränkung auf Type  und Parent_Guid
                            Dim foundRowsLevel3() As DataRow
                            foundRowsLevel3 = DT_TREEVIEW_PER_CONTROLS.Select(expressionLevel3, "SEQUENCE,NODE_CAPTION")
                            Dim l As Integer
                            For l = 0 To foundRowsLevel3.GetUpperBound(0)
                                Dim ID3 = foundRowsLevel3(l)("GUID")
                                Dim LevelEntity3 = foundRowsLevel3(l)("ENTITY_ID")
                                ClassNodeNavigation.Check_NODE_CONFIG_ID(LevelEntity3)
                                Dim controlID3 = foundRowsLevel3(l)("CONTROL_ID")
                                Dim nodetext3 = foundRowsLevel3(l)("NODE_CAPTION")
                                RECORD_ID = foundRowsLevel3(l)("RECORD_ID")
                                NODE_CONFIG_ID = foundRowsLevel3(l)("NODE_CONFIG_ID")
                                ' Node erstellen..
                                Dim LEVEL3_NODE As TreeNode
                                LEVEL3_NODE = New TreeNode(nodetext3)
                                LEVEL3_NODE.Tag = CreateNODE_TAG(LevelEntity, ID, RECORD_ID, NODE_CONFIG_ID)

                                index = 0
                                For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                    If img = NODE_CONFIG_ID & "#" & LevelEntity3 Then
                                        Exit For
                                    End If
                                    index += 1
                                Next
                                LEVEL3_NODE.ImageIndex = index
                                LEVEL3_NODE.SelectedImageIndex = index
                                ' ..und einfügen
                                LEVEL2_NODE.Nodes.Add(LEVEL3_NODE)
                                '### LEVEL 4 laden #######
                                Dim expressionLevel4 As String
                                expressionLevel4 = "PARENT_GUID = " & ID3 'TYPE_NODE = 4 AND Einschränkung auf Type  und Parent_Guid
                                Dim foundRowsLevel4() As DataRow
                                foundRowsLevel4 = DT_TREEVIEW_PER_CONTROLS.Select(expressionLevel4, "SEQUENCE,NODE_CAPTION")
                                Dim m As Integer
                                For m = 0 To foundRowsLevel4.GetUpperBound(0)
                                    Dim ID5 = foundRowsLevel4(m)("GUID")
                                    Dim LevelEntity5 = foundRowsLevel4(m)("ENTITY_ID")
                                    ClassNodeNavigation.Check_NODE_CONFIG_ID(LevelEntity5)
                                    Dim controlID5 = foundRowsLevel4(m)("CONTROL_ID")
                                    Dim nodetext5 = foundRowsLevel4(m)("NODE_CAPTION")
                                    RECORD_ID = foundRowsLevel4(m)("RECORD_ID")
                                    NODE_CONFIG_ID = foundRowsLevel4(m)("NODE_CONFIG_ID")
                                    ' Node erstellen..
                                    Dim LEVEL4_NODE As TreeNode
                                    LEVEL4_NODE = New TreeNode(nodetext5)
                                    LEVEL4_NODE.Tag = CreateNODE_TAG(LevelEntity, ID, RECORD_ID, NODE_CONFIG_ID)

                                    index = 0
                                    For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                        If img = NODE_CONFIG_ID & "#" & LevelEntity5 Then
                                            Exit For
                                        End If
                                        index += 1
                                    Next
                                    LEVEL4_NODE.ImageIndex = index
                                    LEVEL4_NODE.SelectedImageIndex = index
                                    ' ..und einfügen
                                    LEVEL3_NODE.Nodes.Add(LEVEL4_NODE)
                                    '### LEVEL 5 laden #######
                                    Dim expressionLevel5 As String
                                    Dim sortExp As String = "NODE_CONFIG_ID, SEQUENCE"
                                    expressionLevel5 = "PARENT_GUID = " & ID5 'TYPE_NODE = 5 AND  Einschränkung auf Type  und Parent_Guid
                                    Dim foundRowsLevel5() As DataRow
                                    foundRowsLevel5 = DT_TREEVIEW_PER_CONTROLS.Select(expressionLevel5, sortExp)
                                    Dim n As Integer
                                    If ID5 = 1146 Then
                                        Console.WriteLine("Aha")
                                    End If
                                    For n = 0 To foundRowsLevel5.GetUpperBound(0)
                                        Dim ID6 = foundRowsLevel5(n)("GUID")
                                        Dim LevelEntity6 = foundRowsLevel5(n)("ENTITY_ID")
                                        ClassNodeNavigation.Check_NODE_CONFIG_ID(LevelEntity6)
                                        Dim controlID6 = foundRowsLevel5(n)("CONTROL_ID")
                                        Dim nodetext6 = foundRowsLevel5(n)("NODE_CAPTION")
                                        RECORD_ID = foundRowsLevel5(n)("RECORD_ID")
                                        NODE_CONFIG_ID = foundRowsLevel5(n)("NODE_CONFIG_ID")
                                        ' Node erstellen..
                                        Dim LEVEL5_NODE As TreeNode
                                        LEVEL5_NODE = New TreeNode(nodetext6)
                                        LEVEL5_NODE.Tag = CreateNODE_TAG(LevelEntity, ID, RECORD_ID, NODE_CONFIG_ID)

                                        index = 0
                                        For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                            If img = NODE_CONFIG_ID & "#" & LevelEntity6 Then
                                                Exit For
                                            End If
                                            index += 1
                                        Next
                                        LEVEL5_NODE.ImageIndex = index
                                        LEVEL5_NODE.SelectedImageIndex = index
                                        ' ..und einfügen
                                        LEVEL4_NODE.Nodes.Add(LEVEL5_NODE)
                                        '### LEVEL 5 laden #######
                                        Dim expressionLevel6 As String
                                        expressionLevel6 = "PARENT_GUID = " & ID6 'TYPE_NODE = 6 AND  Einschränkung auf Type  und Parent_Guid
                                        Dim foundRowsLevel6() As DataRow
                                        foundRowsLevel6 = DT_TREEVIEW_PER_CONTROLS.Select(expressionLevel6, sortExp)
                                        If ID6 = 1146 Then
                                            Console.WriteLine("Aha")
                                        End If
                                        Dim o As Integer
                                        For o = 0 To foundRowsLevel6.GetUpperBound(0)
                                            Dim ID7 = foundRowsLevel6(o)("GUID")
                                            Dim LevelEntity7 = foundRowsLevel6(o)("ENTITY_ID")
                                            ClassNodeNavigation.Check_NODE_CONFIG_ID(LevelEntity7)
                                            Dim controlID7 = foundRowsLevel6(o)("CONTROL_ID")
                                            Dim nodetext7 = foundRowsLevel6(o)("NODE_CAPTION")
                                            RECORD_ID = foundRowsLevel6(o)("RECORD_ID")
                                            NODE_CONFIG_ID = foundRowsLevel6(o)("NODE_CONFIG_ID")
                                            ' Node erstellen..
                                            Dim LEVEL6_NODE As TreeNode
                                            LEVEL6_NODE = New TreeNode(nodetext7)

                                            LEVEL6_NODE.Tag = CreateNODE_TAG(LevelEntity, ID, RECORD_ID, NODE_CONFIG_ID)

                                            index = 0
                                            For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                                If img = NODE_CONFIG_ID & "#" & LevelEntity7 Then
                                                    Exit For
                                                End If
                                                index += 1
                                            Next
                                            LEVEL6_NODE.ImageIndex = index
                                            LEVEL6_NODE.SelectedImageIndex = index
                                            ' ..und einfügen
                                            LEVEL5_NODE.Nodes.Add(LEVEL6_NODE)
                                            '### LEVEL 7 laden #######
                                            Dim expressionLevel7 As String
                                            expressionLevel7 = "PARENT_GUID = " & ID7 'TYPE_NODE = 7 AND Einschränkung auf Type  und Parent_Guid
                                            Dim foundRowsLevel7() As DataRow
                                            foundRowsLevel7 = DT_TREEVIEW_PER_CONTROLS.Select(expressionLevel7, sortExp)
                                            Dim p As Integer
                                            For p = 0 To foundRowsLevel7.GetUpperBound(0)
                                                Dim ID8 = foundRowsLevel7(p)("GUID")
                                                Dim LevelEntity8 = foundRowsLevel7(p)("ENTITY_ID")
                                                ClassNodeNavigation.Check_NODE_CONFIG_ID(LevelEntity8)
                                                Dim controlID8 = foundRowsLevel7(p)("CONTROL_ID")
                                                Dim nodetext8 = foundRowsLevel7(p)("NODE_CAPTION")
                                                RECORD_ID = foundRowsLevel7(p)("RECORD_ID")
                                                NODE_CONFIG_ID = foundRowsLevel7(p)("NODE_CONFIG_ID")
                                                ' Node erstellen..
                                                Dim LEVEL7_NODE As TreeNode
                                                LEVEL7_NODE = New TreeNode(nodetext8)
                                                LEVEL7_NODE.Tag = CreateNODE_TAG(LevelEntity, ID, RECORD_ID, NODE_CONFIG_ID)

                                                index = 0
                                                For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                                    If img = NODE_CONFIG_ID & "#" & LevelEntity8 Then
                                                        Exit For
                                                    End If
                                                    index += 1
                                                Next
                                                LEVEL7_NODE.ImageIndex = index
                                                LEVEL7_NODE.SelectedImageIndex = index
                                                ' ..und einfügen
                                                LEVEL6_NODE.Nodes.Add(LEVEL7_NODE)
                                                '### LEVEL 7 laden #######
                                                Dim expressionLevel8 As String
                                                expressionLevel8 = "PARENT_GUID = " & ID8 'TYPE_NODE = 7 AND  Einschränkung auf Type  und Parent_Guid
                                                Dim foundRowsLevel8() As DataRow
                                                foundRowsLevel8 = DT_TREEVIEW_PER_CONTROLS.Select(expressionLevel8, sortExp)
                                                Dim q As Integer
                                                For q = 0 To foundRowsLevel8.GetUpperBound(0)
                                                    Dim ID9 = foundRowsLevel8(q)("GUID")
                                                    Dim LevelEntity9 = foundRowsLevel8(q)("ENTITY_ID")
                                                    ClassNodeNavigation.Check_NODE_CONFIG_ID(LevelEntity9)
                                                    Dim controlID9 = foundRowsLevel8(q)("CONTROL_ID")
                                                    Dim nodetext9 = foundRowsLevel8(q)("NODE_CAPTION")
                                                    RECORD_ID = foundRowsLevel8(q)("RECORD_ID")
                                                    NODE_CONFIG_ID = foundRowsLevel8(q)("NODE_CONFIG_ID")
                                                    ' Node erstellen..
                                                    Dim LEVEL8_NODE As TreeNode
                                                    LEVEL8_NODE = New TreeNode(nodetext9)
                                                    LEVEL8_NODE.Tag = CreateNODE_TAG(LevelEntity, ID, RECORD_ID, NODE_CONFIG_ID)
                                                    index = 0
                                                    For Each img As String In TREEVIEW_IMAGELIST.Images.Keys
                                                        If img = NODE_CONFIG_ID & "#" & LevelEntity9 Then
                                                            Exit For
                                                        End If
                                                        index += 1
                                                    Next
                                                    LEVEL8_NODE.ImageIndex = index
                                                    LEVEL8_NODE.SelectedImageIndex = index
                                                    ' ..und einfügen
                                                    LEVEL7_NODE.Nodes.Add(LEVEL8_NODE)
                                                    '### LEVEL 8 laden #######
                                                Next ' Level 7 Ende
                                            Next ' Level 7 Ende
                                        Next ' Level 6 Ende
                                    Next ' Level 5 Ende
                                Next 'Level 4 Ende
                            Next 'Level 3 Ende
                        Next ' Level 2 Ende
                    Next 'Level 1 Ende
                Next
                mytrv.Nodes.Add(LEVEL0_NODE)
            Else
                MsgBox("No Nodes for Navigation. Check the Control Navigation Option or inform Digital Data!", MsgBoxStyle.Critical)
                Return Nothing
            End If
            Return mytrv
        Catch ex As Exception
            ClassLogger.Add(">> Unexpected Error in CreateTreeViewNodes (NodeNavigation): " & ex.Message, True)
            MsgBox("Error in CreateTreeViewNodes (NodeNavigation):" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function
    Public Shared Function Return_ENTITY_FOR_TAG(NodeString As String)

        If IsNumeric(NodeString) Then
            Return NodeString
        Else
            Try
                Dim staticListItems = NodeString.Split("#")
                Return staticListItems(1)
            Catch ex As Exception
                ClassLogger.Add("Unexpected Error in Return_ENTITY_FOR_TAG ('" & NodeString & "'): " & ex.Message, True)
                Return 0
            End Try
        End If
    End Function
    Public Shared Function Return_RECORD_forTag(NodeString As String)
        If IsNumeric(NodeString) Then
            Return NodeString
        Else
            Dim staticListItems = NodeString.Split("#")
            If staticListItems(3).Contains("RECORD-ID") Then
                Try
                    Dim _RECORD = staticListItems(3).ToString.Replace("RECORD-ID", "")
                    If IsNumeric(_RECORD) Then
                        Return _RECORD
                    Else
                        Return 0
                    End If
                Catch ex As Exception
                    ClassLogger.Add("Unexpected Error in Return_RECORD_forTag ('" & NodeString & "'): " & ex.Message, True)
                    Return 0
                End Try
            Else
                Return 0
            End If

        End If
    End Function
    Public Shared Function Return_NODEID_forTag(NodeString As String)
        'If NODE_CONFIGURABLE_ID = NODE_CONFIG_ID Then
        '    vType = "CONFIG_NODE-" & NODE_CONFIG_ID.ToString
        'Else
        '    vType = "NODE-TYPE-" & NODE_CONFIG_ID.ToString
        'End If
        'If Not IsDBNull(Record) Then
        '    TagString = vType & "#" & Entity & "#" & "NODE_ID-" & IDNode & "#RECORD-ID" & Record.ToString
        'Else
        '    TagString = vType & "#" & Entity & "#" & "NODE_ID-" & IDNode
        'End If
        If IsNumeric(NodeString) Then
            Return NodeString
        Else
            Dim staticListItems = NodeString.Split("#")
            If staticListItems(2).Contains("NODE_ID") Then
                Return staticListItems(2).ToString.Replace("NODE_ID-", "")
            Else
                Return 0
            End If

        End If
    End Function
End Class
