VB.net Tutorials with Examples

VB.Net Projects

VB.Net Project 1

adplus-dvertising
Simple Code For Edit Update And Delete In Gridview
Previous Home Next

Design of EDIT Gridview

How to Edit Gridview

Protected Sub GridView1_RowEditing(ByVal sender As Object, 
ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) 
Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
bindgrid()
 End Sub

Design of Update in Gridview

How to Update Gridview

Protected Sub GridView1_RowUpdating(ByVal sender As Object, 
ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) 
Handles GridView1.RowUpdating
Dim UserID As String = GridView1.DataKeys(e.RowIndex).Value.ToString()
Dim l As String = DirectCast(GridView1.Rows(e.RowIndex).Cells(1).Controls(0), 
TextBox).Text
Dim k As String = DirectCast(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), 
TextBox).Text
Dim can As New OleDbConnection("provider=microsoft.jet.oledb.4.0;
Data source=D:\invent.mdb")
can.Open()
Dim com1 As New OleDbCommand("update purchase set purchase1='" & l 
& "',tax='" & k & "' where item_id='" & UserID & "'", can)
com1.ExecuteNonQuery()
can.Close()
'to go back to the previous position
GridView1.EditIndex = -1
' // fetch and rebind the data.
bindgrid()
End Sub

Note: item_id must be primary key And right click on gridview go to property datakey names=item_id(primary key)

Design of Delete Row from Gridview

How to Delete Particular Row From Gridview

This event shows how to delete a row from datebase

Protected Sub GridView1_RowDeleting(ByVal sender As Object, 
ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) 
Handles GridView1.RowDeleting
Dim UserID As String = GridView1.DataKeys(e.RowIndex).Value.ToString()
Dim can As New OleDbConnection("provider=microsoft.jet.oledb.4.0;
Data source=D:\invent.mdb")
can.Open()
Dim com1 As New OleDbCommand("delete from purchase where item_id ='" 
& UserID & "'", can)
com1.ExecuteNonQuery()
can.Close()
'to go back to the previous position
GridView1.EditIndex = -1
' // fetch and rebind the data.
bindgrid()
End Sub

How to cancel editable mode.

Protected Sub GridView1_RowEditing(ByVal sender As 
Object, 
ByVal e As System.
Web.UI.
WebControls.GridViewEditEventArgs) 
Handles GridView1.RowEditing
 GridView1.EditIndex = e.NewEditIndex
 bindgrid()
 End Sub
Previous Home Next