The following is the table design from where I am fetching employee records.

employee records
Image 1.

  1. CREATE TABLE [dbo].[Employee](
  2. [Emp_ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [varchar](50) NULL,
  4. [Email] [varchar](500) NULL,
  5. [Designation] [varchar](50) NULL,
  6. [City] [varchar](50) NULL,
  7. [State] [varchar](50) NULL,
  8. [Country] [varchar](50) NULL,
  9. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
  10. (
  11. [Emp_ID] ASC
  12. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  13. ) ON [PRIMARY]
  14. GO

The following is the data in my table:

table
Image 2.

Now my aspx is:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Delete Multiple Records In Grid View Using Check Box in ASP.NET C#</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <table style="border: solid 15px blue; width: 100%; vertical-align: central;">
  11. <tr>
  12. <td style="padding-left: 50px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue;
  13. font-size: 20pt; color: orangered;">
  14. Delete Multiple Records In Grid View Using Check Box in ASP.NET C#
  15. </td>
  16. </tr>
  17. <tr>
  18. <td style="text-align: left; padding-left: 50px; border: solid 1px red;">
  19. <asp:GridView ID="GridViewEmployee" runat="server" DataKeyNames="Emp_ID" AutoGenerateColumns="False"
  20. Width="90%" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
  21. CellPadding="4" GridLines="Both">
  22. <Columns>
  23. <asp:TemplateField HeaderText="Select">
  24. <ItemTemplate>
  25. <asp:CheckBox ID="chkSelect" runat="server" />
  26. </ItemTemplate>
  27. </asp:TemplateField>
  28. <asp:BoundField DataField="Name" HeaderText="Employee Name" />
  29. <asp:BoundField DataField="Email" HeaderText="Email" />
  30. <asp:BoundField DataField="Designation" HeaderText="Designation" />
  31. <asp:BoundField DataField="City" HeaderText="City" />
  32. <asp:BoundField DataField="State" HeaderText="State" />
  33. <asp:BoundField DataField="Country" HeaderText="Country" />
  34. </Columns>
  35. <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
  36. <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
  37. <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
  38. <RowStyle BackColor="White" ForeColor="#003399" />
  39. <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
  40. </asp:GridView>
  41. </td>
  42. </tr>
  43. <tr>
  44. <td align="right">
  45. <asp:Button ID="btnDeleteRecords" Text="Delete Records" OnClick="btnDeleteRecords_Click"
  46. runat="server" />
  47. </td>
  48. </tr>
  49. </table>
  50. </div>
  51. </form>
  52. </body>
  53. </html>

My aspx.cs code is:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. public partial class _Default : System.Web.UI.Page
  10. {
  11. SqlDataAdapter da;
  12. DataSet ds = new DataSet();
  13. DataTable dt = new DataTable();
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. if (!Page.IsPostBack)
  17. this.BindGrid();
  18. }
  19. private void BindGrid()
  20. {
  21. SqlConnection con = new SqlConnection();
  22. ds = new DataSet();
  23. con.ConnectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;";
  24. SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE", con);
  25. da = new SqlDataAdapter(cmd);
  26. da.Fill(ds);
  27. con.Open();
  28. cmd.ExecuteNonQuery();
  29. con.Close();
  30. if (ds.Tables[0].Rows.Count > 0)
  31. {
  32. GridViewEmployee.DataSource = ds.Tables[0];
  33. GridViewEmployee.DataBind();
  34. }
  35. }
  36. protected void btnDeleteRecords_Click(object sender, EventArgs e)
  37. {
  38. DataTable dt = new DataTable();
  39. dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name", typeof(string)),
  40. new DataColumn("Email",typeof(string)) });
  41. foreach (GridViewRow row in GridViewEmployee.Rows)
  42. {
  43. if ((row.FindControl("chkSelect") as CheckBox).Checked)
  44. {
  45. int Emp_ID = Convert.ToInt32(GridViewEmployee.DataKeys[row.RowIndex].Value);
  46. using (SqlConnection con = new SqlConnection(@"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;"))
  47. {
  48. con.Open();
  49. SqlCommand cmd = new SqlCommand("DELETE FROM Employee WHERE Emp_ID=" + Emp_ID, con);
  50. cmd.ExecuteNonQuery();
  51. con.Close();
  52. }
  53. }
  54. }
  55. this.BindGrid();
  56. }
  57. }

Now run the application:

run
Image 3

Now select some records using the checkboxes and click on the "Delete Records" button.

delet record
Image 4.

output
Image 5.