Hey Buddy,
The Update event:
At first, you need to create the OnRowUpdating Event from the event window of the GridView:
Protected Sub DataGridView_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
Dim ID As Label = TryCast(DataGridView.Rows(e.RowIndex).FindControl("LblStuID"), Label)
Dim Name As TextBox = TryCast(DataGridView.Rows(e.RowIndex).FindControl("TxtEditStuName"), TextBox)
Dim Gender As DropDownList = TryCast(DataGridView.Rows(e.RowIndex).FindControl("DDLEditStuGender"), DropDownList)
Dim Grad As TextBox = TryCast(DataGridView.Rows(e.RowIndex).FindControl("TxtEditStuGraduation"), TextBox)
Dim PostGrad As TextBox = TryCast(DataGridView.Rows(e.RowIndex).FindControl("TxtEditStuPostGraduation"), TextBox)
Dim Mobile As TextBox = TryCast(DataGridView.Rows(e.RowIndex).FindControl("TxtEditStuMobileNo"), TextBox)
Dim Email As TextBox = TryCast(DataGridView.Rows(e.RowIndex).FindControl("TxtEditStuEmailID"), TextBox)
Dim Message As String = obj.Updata_Data(ID, Name, Gender.SelectedItem, Grad, PostGrad, Mobile, _
Email)
Fill_Grid()
Literal1.Text = Message
End Sub
Update_Data definition is:
Public Function Updata_Data(ID As Label, Name As TextBox, listItem As ListItem, Grad As TextBox, PostGrad As TextBox, Mobile As TextBox, _
Email As TextBox) As String
cmd = New SqlCommand("Update Tbl_Student set Stu_Name='" & Convert.ToString(Name.Text) & "', Stu_Gender='" & Convert.ToString(listItem.Text) & "', Stu_Graduation='" & Convert.ToString(Grad.Text) & "', Stu_PostGraduation='" & Convert.ToString(PostGrad.Text) & "', Stu_MobileNo=" & Convert.ToString(Mobile.Text) & ", Stu_EmailID='" & Convert.ToString(Email.Text) & "' where Stu_ID=" & Convert.ToString(ID.Text) & "", con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Return "Updated Sucessfully"
End Function
The Delete Event:
At first, you need to create the OnRowDeleting Event from the event window of the GridView:
Protected Sub DataGridView_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
Dim Stu_ID As Label = DirectCast(DataGridView.Rows(e.RowIndex).FindControl("LblStuID"), Label)
Dim id As Integer = Convert.ToInt32(Stu_ID.Text)
Dim Message As String = obj.Delete_Data(id)
Fill_Grid()
Literal1.Text = Message
End Sub
The Delete_Data definition is:
Public Function Delete_Data(id As Integer) As String
con.Open()
cmd = New SqlCommand("Delete from Tbl_Student where Stu_ID=" & id & "", con)
cmd.ExecuteNonQuery()
con.Close()
Return "Deleted Sucessfully"
End Function
Note: Please modify the code with your details.