Delete row/rows from Gridview in WebForms
Simple code to delete row from Gridview in Web Forms. Checkboxes in each row and selected row values will be deleted from the table.
Just need to Drag and Drop Gridview on the page. Bind it to database. Place a checkbox in Item template part of gridview.
Place a button below Gridview and on button click follow the code.
Dim conn As New Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Admin\My Documents\Visual Stdio 2005\Websites\mp3\App_Data\Database.mdf;Integrated Security=True;User Instance=True")
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Integer
Dim cc As New CheckBox()
For i = 0 To GridView1.Rows.Count - 1
cc = GridView1.Rows(i).Cells(0).Controls(1)
If cc.Checked = True Then
Dim command1 As New Data.SqlClient.SqlCommand("delete from feedback where id = '" + GridView1.Rows(i).Cells(1).Text + " '", conn)
conn.Open()
command1.ExecuteNonQuery()
conn.Close()
Response.Write("deleted")
End If
Next
Dim command As New Data.SqlClient.SqlCommand("select * from feedback", conn)
Dim ds As New Data.DataSet
Dim da As New Data.SqlClient.SqlDataAdapter(command)
conn.Open()
da.Fill(ds)
conn.Close()
GridView1.DataSource = ds
GridView1.DataBind()
TextBox1.Text = GridView1.Rows.Count.ToString
End Sub
nice example...its working