VB.NET - How To Update A DataGridView Row Using InputBox In VBNET

datagridview update row

VB.NET - How To Update DataGridView Selected Row With Using InputBox In VBNET 


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

Project Source Code:

Public Class VB_Update_DataGridView_Row_Using_InputBox

    Dim table As New DataTable("Table")

    ' selected datagridview row index
    Dim selectedRowIndex As Integer

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

        ' populate datagridview from datatable
        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"))

        table.Rows.Add(1, "HJK", "KJH", 34)
        table.Rows.Add(2, "ERT", "IYT", 86)
        table.Rows.Add(3, "CVBN", "NBVC", 22)
        table.Rows.Add(4, "SDFGH", "HGFDS", 12)
        table.Rows.Add(5, "AZERTY", "YTREZA", 52)
        table.Rows.Add(6, "POIU", "UIOP", 31)
        table.Rows.Add(7, "AZSDFVB", "VCRTYUXA", 45)

        DataGridView1.DataSource = table

    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        ' get the index of the selected row
        selectedRowIndex = e.RowIndex

    End Sub

    Private Sub BtnUpdateRow_Click(sender As Object, e As EventArgs) Handles BtnUpdateRow.Click

        Dim row As New DataGridViewRow()
        row = DataGridView1.Rows(selectedRowIndex)

        ' get data from inputbox
        Dim id As String
        id = InputBox("Enter The New Id", "New Data", row.Cells(0).Value.ToString())

        Dim fn As String
        fn = InputBox("Enter The New First Name", "New Data", row.Cells(1).Value.ToString())

        Dim ln As String
        ln = InputBox("Enter The New Last Name", "New Data", row.Cells(2).Value.ToString())

        Dim age As String
        age = InputBox("Enter The New age", "New Data", row.Cells(3).Value.ToString())

        ' insert row inside the row
        row.Cells(0).Value = Integer.Parse(id)
        row.Cells(1).Value = fn
        row.Cells(2).Value = ln
        row.Cells(3).Value = Integer.Parse(age)

    End Sub
End Class


update datagridview selected row
VB.NET Update DataGridView Row Using InputBox

Share this

Related Posts

Next Post »