C1Treeview VB sample

Posted by: saidnai on 29 January 2021, 12:30 pm EST

    • Post Options:
    • Link

    Posted 29 January 2021, 12:30 pm EST

    Dear all,

    I need to show structured data from SQL database in a treeview and be able to manage the nodes with a popup menu on right mouse click on the node(Insert/Update/Delete).

    such as structur:

    1

    1.1

    1.1.1

    1.1.2

    e.t.c

    1.2

    1.2.1

    1.2.2

    e.t.c

    2
    	2.1
    		2.1.1
    		2.1.2
    		e.t.c
    	2.2
    		2.2.1
    		2.2.2
    		e.t.c
    

    It will be great if you can provide a VB sample that i can work on it.

    Best regards

    Said

  • Posted 2 February 2021, 8:53 am EST

    Hi,

    You can bind SQL data to TreeView using DataTable. You need to fill in the data table from the database and assign it as the data source. You also need to set the DisplayFieldNames for the columns in the TreeView. For more info please refer to https://www.grapecity.com/componentone/docs/win/online-treeview/DataBinding.html?highlight=dataset%2C and the sample “BoundData” located at “C:\Users\username\Documents\ComponentOne Samples\WinForms\v4.5.2\TreeView\VB\BoundModeWithDataSet”.

    For the changes to be updated in the data table you also need to set the BindingMode property of the BindingInfo of the Treeview to TwoWay.

    Now, to show the popup menu on right-click, you can handle the mouse down event and then show a context menu at the location of the click. Then you can use the GetNodeAtPoint() method to get the node at the location of the click.

    Once you have the node that was clicked, you can perform the desired operations. For deletion, you can use the ParentCollection property of the Node and then remove the node from that using the Remove() method. To insert a node in bound mode, you need to add a record to the data source directly and then reassign the data source to the treeview to show the changes. In the case of a data table, you need to add a data row with all the fields required for the hierarchy. For updation you can easily use the SetValue of the node to set different values. All the changes will be updated in the data table, then on the FormClosing event, you can update the changes back to the database. Please refer to the sample attached and modify it according to your requirements, we have used access database to fill the data table, you can easily use SQL to do the same.

    If you have any questions, please let us know.

    Regards.

    AvnishBoundModeWithDataSet_mod.zip

  • Posted 5 February 2021, 12:27 pm EST

    Dear Kumar,

    For the Treeview i miss some events such as on any node click i need to get the ID that i assigned from the table when generating the nodes. The same will be for mouse up and down on the Treeview. I planned to show other data from the table depending on the clicked node or even do an update record. because the ID of the node is the ID of the record in the database.

    Best regards

    Said

  • Posted 8 February 2021, 3:01 am EST - Updated 3 October 2022, 11:23 pm EST

    Hi Said,

    To get the node that was clicked you can use the GetNodeAtPoint() method of the Treeview in MouseUp or MouseDown event. Then you can use the GetValue() method of the Node to get the data source entry associated with the node. In case of binding with a DataTable, you will get a DataRow object. Once you have the data row, you can get the value of any field including the ID field.

    Please refer to the GIF and modified sample attached.

    Regards.

    Avnish

    BoundModeWithDataSet_mod00.zip

  • Posted 8 February 2021, 6:02 am EST

    Dear Kumar,

    Thank a lot for your support.

    Just want to explain, what i want to do. I have 5 tables, the first table Level1 is the parent for the main product. form Table Level2 to Table Level5 are child and contains properties of the product. Each Level has a PDF document as a specification that should be shown on the right side.

    So I need the onclick of each Level to get the node ID (= LevelX_ID) to be able to read the document Name of it and open it as said on the Right. Here i guess there is C1component that can do it like C1Flexviewer.

    I have joind a VB test project including the DB.

    again i appreciat your support

    Said

  • Posted 8 February 2021, 6:03 am EST

    Here is the sample project.

    Best regards

    Said

  • Posted 8 February 2021, 6:17 am EST

    again. I had to delete the following DLLs to the 5mb:

    C1.Win.4.dll

    C1.Win.C1Input.4.dll

    C1.Win.TreeView.4.dll

    BoundModeWithDataSet.zip

  • Posted 8 February 2021, 9:45 am EST

    Dear Kumar,

    I change the table logic to 1 table. I think i can also be done with becasue of of the unique ID of the record. this will be than the Node ID to read.

    New Code:[ul][/ul]

    'create parent nodes

    SqlStr1 = “Select Level_ID, Level1, Level1Text From Table1 Where Level1 > 0 Order by Level1”

    For Each rstL1 As DataRow In ExecuteSQL(SqlStr1).Tables(0).Rows

    Dim parentNode1 As New C1.Win.TreeView.C1TreeNode()

    C1TreeView1.Nodes.Add(parentNode1)

    parentNode1.SetValue(rstL1.Item(“Level1Text”), 0)

                'create child1 nodes
                SqlStr2 = "Select Level_ID, Level2, Level2Text From Table1 Where Level2 = " & rstL1.Item("Level_ID") & " Order by Level2Text"
                For Each rstL2 As DataRow In ExecuteSQL(SqlStr2).Tables(0).Rows
                    Dim childNode1 As New C1.Win.TreeView.C1TreeNode()
                    parentNode1.Nodes.Add(childNode1)
                    childNode1.SetValue(rstL2.Item("Level2Text"), 0)
    
                    'create child2 nodes
                    SqlStr3 = "Select Level_ID, Level3, Level3Text From Table1 Where Level3 = " & rstL2.Item("Level_ID") & " Order by Level3Text"
                    For Each rstL3 As DataRow In ExecuteSQL(SqlStr3).Tables(0).Rows
                        Dim childNode2 As New C1.Win.TreeView.C1TreeNode()
                        childNode1.Nodes.Add(childNode2)
                        childNode2.SetValue(rstL3.Item("Level3Text"), 0)
    
                        'create child3 nodes
                        SqlStr4 = "Select Level_ID, Level4, Level4Text From Table1 Where Level4 = " & rstL3.Item("Level_ID") & " Order by Level4Text"
                        For Each rstL4 As DataRow In ExecuteSQL(SqlStr4).Tables(0).Rows
                            Dim childNode3 As New C1.Win.TreeView.C1TreeNode()
                            childNode2.Nodes.Add(childNode3)
                            childNode3.SetValue(rstL4.Item("Level4Text"), 0)
    
                            'create child4 nodes
                            SqlStr5 = "Select Level_ID, Level5, Level5Text From Table1 Where Level5 = " & rstL4.Item("Level_ID") & " Order by Level5Text"
                            For Each rstL5 As DataRow In ExecuteSQL(SqlStr5).Tables(0).Rows
                                Dim childNode4 As New C1.Win.TreeView.C1TreeNode()
                                childNode3.Nodes.Add(childNode4)
                                childNode4.SetValue(rstL5.Item("Level5Text"), 0)
                            Next
                        Next
                    Next
                Next
            Next
    

    The Problem still with onMouseClick, Up and Down Cursor and how to Show the pdf on the Right side…

    Best regards

    Said

    DemoData_New.zip

  • Posted 9 February 2021, 4:41 am EST

    Hi Said,

    Thank You for providing the sample. Since you are creating Nodes manually, you can store the ID in the Tag property of the node, while creating the node. Now, when a node is clicked you can get the ID from the Tag property. Once you have the ID you can use it to get the corresponding document name from the database and load the required document. Please refer to the modified sample attached.

    If you have any questions, please let us know.

    Regards.

    Avnish

    BoundModeWithDataSet_mod000.zip

  • Posted 9 February 2021, 10:10 am EST

    Dear Avnish,

    Well done :)[ul][/ul]

    I still miss the keydown Event on the C1Treeview. The following code wiil not be fired: to get the node ID when the keydown/Keyup or Enter is pusched:

    Private Sub C1TreeView1_KeyDown(sender As Object, e As KeyEventArgs) Handles C1TreeView1.KeyDown
        Try
            If CType(e.KeyCode, Integer) = 38 Or CType(e.KeyCode, Integer) = 40 Or CType(e.KeyCode, Integer) = 13 Then   'UP = 38, Down = 40, Enter = 13
                If CType(e.KeyCode, Integer) = 38 Then   'UP = 38
                                      MsgBox("(Mouse Up)Node's ID: " & GetNodeID(e.Location))
                ElseIf CType(e.KeyCode, Integer) = 40 Then 'Down = 40
                                       MsgBox("(Mouse Up)Node's ID: " & GetNodeID(e.Location))
                ElseIf CType(e.KeyCode, Integer) = 13 Then 'Enter = 13
                                       MsgBox("(Mouse Up)Node's ID: " & GetNodeID(e.Location))
                End If
            End If
    
        Catch ex As Exception
            MsgBox("Error 'C1TreeView1_KeyDown' : " & vbCrLf & ex.ToString, MsgBoxStyle.Critical)
        End Try
    End Sub
    

    Best regards

    Said

  • Posted 10 February 2021, 2:28 am EST

    Hi Said,

    You can use the PreviewKeyDown event to capture KeyDown. You will not get any Location with key events since that is only provided with Mouse events, you will have to use the GetFocusedNode() method to get the current node.

    Now, since by default C1TreeView allows navigation using keys, you will get the previously selected node in the PreviewKeyDown, as the selected node will change after the key down. If you only need to change the pdf shown when the user navigates with the keys, you can use the SelectionChanged event to show the pdf according to the currently selected node. Please refer to the sample attached.

    Regards.

    Avnish

    BoundModeWithDataSet_mod124.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels