You are on page 1of 1

(22) Visual basic.

net: how to update all data from datagridview to Sql server database at once in
VB.net - YouTube DATAGRIDVIEW UPDATE ROW

Imports System.Data.DataTable

Public Class Update_DataGridView_Row_Using_TextBoxes


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

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, "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)

DataGridView1.DataSource = table

End Sub

Private Sub DataGridView1_CellClick(sender As Object, e As


DataGridViewCellEventArgs) Handles DataGridView1.CellClick

index = e.RowIndex

Dim selectedRow As DataGridViewRow

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
Dim newDataRow As DataGridViewRow
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

You might also like