VB.NET - How To Update A DataGridView Row With TextBox Using VB.NET

VB.NET - How To Update DataGridView Selected Row With TextBoxes In VB.NET 

                                                                                                                         

In This VB.NET Tutorial We Will See How To Update a DataGridView Selected Row Using TextBoxes In VB.NET Programming Language.


Project Source Code:

Imports System.Data.DataTable

Public Class Update_DataGridView_Row_Using_TextBoxes

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

    Dim index As Integer

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

        ' 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, "XXXX", "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)
        'set data from datatable to datagridview
        DataGridView1.DataSource = table

    End Sub
     ' datagridview Cell click event
    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
       ' get the index of the selected datagridview row
        index = e.RowIndex

        Dim selectedRow As DataGridViewRow

        ' display data from datagridview selected row to textboxes

        selectedRow = DataGridView1.Rows(index)

        TextBoxID.Text = selectedRow.Cells(0).Value.ToString()
        TextBoxFN.Text = selectedRow.Cells(1).Value.ToString()
        TextBoxLN.Text = selectedRow.Cells(2).Value.ToString()
        TextBoxAGE.Text = selectedRow.Cells(3).Value.ToString()

    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        ' the new row
        Dim newDataRow As DataGridViewRow
        ' get data from textboxes to the row
        newDataRow = DataGridView1.Rows(index)
        newDataRow.Cells(0).Value = TextBoxID.Text
        newDataRow.Cells(1).Value = TextBoxFN.Text
        newDataRow.Cells(2).Value = TextBoxLN.Text
        newDataRow.Cells(3).Value = TextBoxAGE.Text

    End Sub

End Class

///////////////OUTPUT:

vb.net update datagridview row using textboxes
VB.NET Update Selected DataGridView Row






Share this

Related Posts

Previous
Next Post »