This article shows how to delete multiple records from a GridView using a CheckBox in ASP.NET. This example is helpful in situations where an administrator or user must delete several records from the database. For demonstration I have created a database (named EmployeeDB) in which we have a table named Employee.
Table Schema used in this example:
- CREATE TABLE [dbo].[Employee] (
- [EmpId] INT NOT NULL,
- [FirstName] VARCHAR (20) NOT NULL,
- [LastName] VARCHAR (20) NOT NULL,
- [City] VARCHAR (20) NOT NULL,
- PRIMARY KEY CLUSTERED ([EmpId] ASC)
- );
Let's Begin.
- Drop a GridView Control from the toolbox and set AutoGenerateColumns to false.
- Add the Columns Collection (tag) that manages the collection of column fields.
- Add TemplateField inside the Columns Collection that is used to display custom content in a data-bound control.
- Add an ItemTemplate in the TemplateField that specifies the content to display for the items in a TemplateField.
- Add a CheckBox control inside the ItemTemplate.
- Inside the Columns tag, we have added a column field (BoundField) that displays the value of a field in a data source.
- Add a Button Control (ID="btnDeleteRecord) for deleting the selected/checked records.
Default.aspx code:
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4">
- <Columns>
- <asp:TemplateField>
- <ItemTemplate>
- <asp:CheckBox ID="chkDel" runat="server" />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:BoundField DataField="EmpId" HeaderText="Emp Id" />
- <asp:BoundField DataField="FirstName" HeaderText="First Name" />
- <asp:BoundField DataField="LastName" HeaderText="Last Name" />
- <asp:BoundField DataField="City" HeaderText="City" />
- </Columns>
- <HeaderStyle BackColor="#663300" ForeColor="#ffffff" />
- <RowStyle BackColor="#e7ceb6" />
- </asp:GridView>
- <br />
- <asp:Button ID="btnDeleteRecord" runat="server" BackColor="#E7CEB6" Height="32px" OnClick="btnDeleteRecord_Click" Text="Delete" Width="64px" />
- <br />
- </div>
- </form>
We have added JavaScript (a client-side script) so that when the user checks the CheckBox and clicks on the Delete button, a confirmation pop-up comes on the screen and on accepting the confirmation (by clicking on the OK button), a click event of the server-side control fires (btnDeleteRecord_Click) and deletes the checked record.
- <script type="text/javascript">
- function DeleteConfirm()
- {
- var Ans = confirm("Do you want to Delete Selected Employee Record?");
- if (Ans)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- </script>
On Page_Load event, we disaply the data using the showData() method and add an onclick attribute (JavaScript) to the Delete button.
Default.aspx.cs code
-
- string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
- protected void Page_Load(object sender, EventArgs e)
- {
- if(!IsPostBack)
- {
-
- showData();
-
- btnDeleteRecord.Attributes.Add("onclick", "javascript:return DeleteConfirm()");
- }
- }
-
-
- protected void showData()
- {
- DataTable dt = new DataTable();
- SqlConnection con = new SqlConnection(cs);
- SqlDataAdapter adapt = new SqlDataAdapter("select * from Employee",con);
- con.Open();
- adapt.Fill(dt);
- con.Close();
- GridView1.DataSource = dt;
- GridView1.DataBind();
- }
-
- protected void DeleteRecord(int empid)
- {
- SqlConnection con = new SqlConnection(cs);
- SqlCommand com = new SqlCommand("delete from Employee where EmpId=@ID",con);
- com.Parameters.AddWithValue("@ID",empid);
- con.Open();
- com.ExecuteNonQuery();
- con.Close();
- }
-
- protected void btnDeleteRecord_Click(object sender, EventArgs e)
- {
- foreach (GridViewRow grow in GridView1.Rows)
- {
-
- CheckBox chkdel = (CheckBox)grow.FindControl("chkDel");
-
- if(chkdel.Checked)
- {
- int empid = Convert.ToInt32(grow.Cells[1].Text);
- DeleteRecord(empid);
- }
- }
-
- showData();
- }
Final Preview
I hope you like it. Thanks.