This sample shows how to use a Gridview to delete multiple records all at once, having marked them with a Checkbox, and clicking one button, external to the Gridview, to delete them all
As usual, here, we’re using the Northwind Database. If you want to try this on your own Northwind database, adhere to this word of caution – “BACK IT UP FIRST”. You will be making multiple deletes, so in order to get it back to the original state, you must make a backup and then restore afterwards.
Remember too, that you will need to substitute your own Web.Config connectionstring entry for “YourNorthwindString” in the sample code
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<%@ Page Language="VB" %>
<script language="VB" Runat="server">
Dim ConnString as String
Sub Page_Load(Source as Object, E as EventArgs)
ConnString=ConfigurationManager.ConnectionStrings("YourNorthwindString").ConnectionString
if not page.IsPostBack then
BindGrid
end if
End Sub
Sub DeleteRecord(intProd as Integer)
Dim Conn As New SqlConnection(ConnString)
Dim cmd As New SqlCommand("Delete from Products where productID=@ProductID", Conn)
cmd.Parameters.Add(New SqlParameter("@ProductID", intProd))
Conn.Open()
cmd.ExecuteNonQuery()
Conn.Close
End Sub
Sub DoDelete()
Dim i As Integer
For i = 0 To gvProducts.Rows.Count – 1
Dim dgItem As GridViewRow = gvProducts.Rows(i)
Dim lblid As Label = CType(dgItem.FindControl("lblid"), Label)
Dim cb As CheckBox = CType(dgItem.FindControl("chk1"), CheckBox)
If cb.Checked Then
DeleteRecord(CInt(lblid.Text))
End If
Next i
BindGrid()
End Sub
Sub BindGrid()
Dim Conn As New SqlConnection(ConnString)
Dim dr as SqlDataReader
Dim mySQL as String
mySQL="SELECT Top 10 [ProductID], [ProductName], [QuantityPerUnit], " & _
"[UnitPrice] FROM [Products]"
Dim cmd As New SqlCommand(mySQL, Conn)
Conn.Open()
dr=cmd.ExecuteReader
gvProducts.DataSource=dr
gvProducts.DataBind
Conn.Close
End Sub
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
DoDelete()
End Sub
</script>
<head runat="server">
<title>Mass Deletes Using a Gridview with Checkboxes</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False"
DataKeyNames="ProductID">
<Columns>
<asp:TemplateField HeaderText="Mark for Delete">
<ItemTemplate>
<asp:CheckBox ID="chk1" runat="server" Checked="False" />
<asp:Label ID="lblID" Visible="True" runat="server" Text=”<%# Bind("ProductID") %>”></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" HtmlEncode="False" DataFormatString="{0:C}" SortExpression="UnitPrice" />
</Columns>
</asp:GridView>
<asp:Button ID="btnDelete" runat="server" Text="Delete Checked" OnClick="btnDelete_Click" />
</form>
</body>
</html>