VB.NET - How To Add Delete And Update Row From DataGridView Using TextBoxes In VB.NET
In This VB.NET Tutorial We Will See How To Add A Row To DataGridView From TextBox, Update A Selected Row Using TextBox, And Delete The Selected Row In VB.NET Programming Language.
Project Source Code:
Imports System.Data.DataTable
Public Class Add_Update_Delete_DataGridView_Row
' Create a new datatable
Dim table As New DataTable("Table")
Dim index As Integer
Private Sub Add_Update_Delete_DataGridView_Row_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)
table.Rows.Add(7, "aefht", "cvfhytrff", 21)
table.Rows.Add(8, "wghyuj", "mihgdwrh", 33)
table.Rows.Add(9, "qsztii", "bvdhjh", 53)
table.Rows.Add(10, "rytyufd", "esdfzr", 19)
'set data from datatable to datagridview
DataGridView1.DataSource = table
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
table.Rows.Add(TextBoxID.Text, TextBoxFN.Text, TextBoxLN.Text, TextBoxAGE.Text)
DataGridView1.DataSource = table
End Sub
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
' show data from the 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
newDataRow = DataGridView1.Rows(index)
' get data from textboxes to the row
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
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
DataGridView1.Rows.RemoveAt(index)
End Sub
End Class
///////////////OUTPUT:
vb.net add a row to datagridview |
vb.net update a datagridview row |
vb.net delete a datagridview row |
Bagikan
VB.NET - How To Add Delete And Update DataGridView Row Using TextBoxes In VBNET
4/
5
Oleh
insurance