Popular Posts

VB.Net Transfer DataGridView Data To TreeView

Share it:

How To Set DataGridView Values Into TreeView Nodes Using Visual Basic .Net

Transfer DataGridView Data To TreeView Using VB.Net

In this VB.NET Tutorial we will see How To Get DataGridView Rows Data And Set It Into TreeView Node Using For Loop And Datatable On Button Click Event Using Visual Basic.Net Programming Language And Visual  Studio Editor.





Project Source Code:


Public Class Datagridview_to_TreeView

    Private Sub Datagridview_to_TreeView_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' Create a new datatable
        Dim table As New DataTable("Table")

        ' Add columns to your datatable, 
        ' with the name of the columns and their type 

        table.Columns.Add("Id", Type.GetType("System.Int32"))
        table.Columns.Add("First Name", Type.GetType("System.String"))
        table.Columns.Add("Last Name", Type.GetType("System.String"))
        table.Columns.Add("Age", Type.GetType("System.Int32"))


        ' Add rows to the datatable with some data

        table.Rows.Add(1, "BBBB", "YYYYY", 21)
        table.Rows.Add(2, "SSDD", "hGSQ", 33)
        table.Rows.Add(3, "fgfgd", "jgfdd", 53)
        table.Rows.Add(4, "cvfghyghj", "sdrgtyh", 19)
        table.Rows.Add(5, "hghfd", "ghjgdf", 36)
        table.Rows.Add(6, "cvvdfgh", "juyrfdvc", 63)

        ' now set the datagridview datasource equals to your datatable name

        DataGridView1.DataSource = table

    End Sub

    ' button transfer
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' go through all datagridview rows
        For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1

            Dim rowIndex As Integer = i + 1

            ' create a new NODE
            Dim node As New TreeNode("Row Index => " + rowIndex.ToString())

            ' go through the current row columns
            For j As Integer = 0 To DataGridView1.Columns.Count - 1 Step +1

                ' add child node to the NODE
                node.Nodes.Add(DataGridView1.Rows(i).Cells(j).Value.ToString())

            Next j

            ' add node to treeview
            TreeView1.Nodes.Add(node)

        Next i

    End Sub
End Class


OutPut:

DataGridView Data To TreeView Using VB.Net



Share it:

dataGridView

Export DataGridView Data To TreeView In VB.Net

treeview

VB.NET

Post A Comment:

0 comments: