We are using a checkbox inside a GridView and a button. The User selects the row from the checkbox that he want to delete and on a button click it is deleted from the GridView and from the database too.

Initial chamber

Step 1

Open Visual Studio 2010 and create an Empty Website, provide a suitable name (Gridview_demo).

Step 2

In Solution Explorer you get your empty website. Add a web form and a SQL Database as in the following.

For Web Form:

Gridview_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it Gridview_demo.aspx.

For SQL Server Database:

Gridview_demo (your empty website) then right-click then select Add New Item -> SQL Server Database. (Add the database inside the App_Data_folder.)

Database chamber

Step 3

In the database (Database.mdf) we will create a table, tbl_Data. Go to the database.mdf -> Table then Add New table, design your table like this:

Table -> tbl_data (Don't forget to make ID -> Identity Specification -> Yes)

table design

Design chamber

Step 4

Now open your Gridview_demo.aspx file, where we create our simple design with the GridView and a button.

Gridview_demo.aspx

  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></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <table style="width:100%;">
  11. <tr>
  12. <td>
  13. </td>
  14. <td>
  15. </td>
  16. <td>
  17. </td>
  18. </tr>
  19. <tr>
  20. <td>
  21. </td>
  22. <td>
  23. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
  24. DataKeyNames="id">
  25. <Columns>
  26. <asp:TemplateField HeaderText="Student_ID">
  27. <EditItemTemplate>
  28. <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>
  29. </EditItemTemplate>
  30. <ItemTemplate>
  31. <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
  32. </ItemTemplate>
  33. </asp:TemplateField>
  34. <asp:TemplateField HeaderText="Student_Name">
  35. <EditItemTemplate>
  36. <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
  37. </EditItemTemplate>
  38. <ItemTemplate>
  39. <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
  40. </ItemTemplate>
  41. </asp:TemplateField>
  42. <asp:TemplateField HeaderText="Student_City">
  43. <EditItemTemplate>
  44. <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
  45. </EditItemTemplate>
  46. <ItemTemplate>
  47. <asp:Label ID="Label3" runat="server" Text='<%# Bind("city") %>'></asp:Label>
  48. </ItemTemplate>
  49. </asp:TemplateField>
  50. <asp:TemplateField>
  51. <EditItemTemplate>
  52. <asp:CheckBox ID="CheckBox1" runat="server" />
  53. </EditItemTemplate>
  54. <ItemTemplate>
  55. <asp:CheckBox ID="CheckBox1" runat="server" />
  56. </ItemTemplate>
  57. </asp:TemplateField>
  58. </Columns>
  59. </asp:GridView>
  60. </td>
  61. <td>
  62. </td>
  63. </tr>
  64. <tr>
  65. <td>
  66. </td>
  67. <td>
  68. <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
  69. </td>
  70. <td>
  71. </td>
  72. </tr>
  73. </table>
  74. </div>
  75. </form>
  76. </body>
  77. </html>
For inserting the checkbox inside the GridView, click on the arrow button of the GridView where you can find Edit Column. In a new window (Fields) you need Checkbox Fields from the Available Fields.

Checkbox Fields

Here is what the design looks like:

design

Code chamber

Step 5

Open your Gridview_demo.aspx.cs and write some code so that the application works.

Gridview_demo.cs
  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. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. if (!Page.IsPostBack)
  15. {
  16. refreshdata();
  17. }
  18. }
  19. public void refreshdata()
  20. {
  21. SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
  22. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  23. DataTable dt = new DataTable();
  24. sda.Fill(dt);
  25. GridView1.DataSource = dt;
  26. GridView1.DataBind();
  27. }
  28. protected void Button1_Click(object sender, EventArgs e)
  29. {
  30. foreach (GridViewRow gvrow in GridView1.Rows)
  31. {
  32. CheckBox chck = gvrow.FindControl("CheckBox1") as CheckBox;
  33. if (chck.Checked)
  34. {
  35. var Label = gvrow.FindControl("Label1") as Label;
  36. SqlCommand cmd = new SqlCommand("delete from tbl_data where id=@id",con);
  37. cmd.Parameters.AddWithValue("id", int.Parse(Label.Text));
  38. con.Open();
  39. int id = cmd.ExecuteNonQuery();
  40. con.Close();
  41. refreshdata();
  42. }
  43. }
  44. }
  45. }


Output chamber

gridview

Don't worry about the Id's that start from 4—8 since I used 1-4 Ids for testing.

delet select row

I hope you like it . Thank you for reading. Have a good day.