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

datagridview add update row

VB.NET - How To Add And Update Row From DataGridView Using InputBoxes In VB.NET 

                                                                                                                         

In This VB.NET Tutorial We Will See How To Add A Row To DataGridView From InputBox, Update A Selected Row Using InputBoxBox In VB.NET Programming Language.


Project Source Code:

Public Class VB_DataGridView_Add_Update_Row_Using_InputBox

    Dim table As New DataTable("Table")

    ' the index of the selected datagridview row
    Dim selectedRowIndex As Integer

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

        ' populate datagridview with some data using 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(3, "CVBN", "NBVC", 22)
        table.Rows.Add(7, "AZSDFVB", "VCRTYUXA", 45)

        DataGridView1.DataSource = table

    End Sub

    ' button add row
    Private Sub BtnAddRow_Click(sender As Object, e As EventArgs) Handles BtnAddRow.Click
        ' get data from inputBox
        Dim id As Integer
        id = Integer.Parse(InputBox("Enter The Id", "Enter Data", -1, -1))

        Dim fn As String
        fn = InputBox("Enter The First Name", "Enter Data", -1, -1)

        Dim ln As String
        ln = InputBox("Enter The Last Name", "Enter Data", -1, -1)

        Dim age As Integer
        age = Integer.Parse(InputBox("Enter The Age", "Enter Data", -1, -1))

        ' insert data into jtable
        table.Rows.Add(id, fn, ln, age)

        DataGridView1.DataSource = table

    End Sub

    ' DataGridView Cell Click
    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

    ' button update row
    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 inputBoxes
        Dim id As String
        id = InputBox("Enter The New Id", "Update Data", row.Cells(0).Value.ToString())

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

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

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

        ' insert data into 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

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


vb.net datagridview add update row
vb.net datagridview add update row



Share this

Related Posts

Previous
Next Post »