title: How can I delete the selected rows (CheckBox used) from the GridView on click of a Button?
body: <p>I have a GridView containing multiple rows (using Sessions) of data extracted from a TextBox. There should be a column on the GridView containing CheckBoxes on every row to select the rows to be deleted. <strong>I have a Button outside the GridView. On click of it, all the selected rows (using CheckBox) from the GridView should be "Deleted" from the GridView. I do not want a "Delete Link" on the GridView.</strong></p>
  
  <p><strong>Please make the necessary changes to the code given below to implement this functionality.</strong></p>
  
  <p><strong>default.aspx</strong></p>
  
  <pre><code>&lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
  
  &lt;asp:Button ID="Button1" runat="server" Text="Add to Grid" OnClick="Button1_Click" /&gt;
          &lt;br /&gt;
  
  &lt;asp:GridView ID="GridView1" runat="server" DataKeyNames="EmpID" AutoGenerateColumns="false"
      OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit"
      OnRowDeleting="GridView1_RowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging"
      PageSize="5" AllowPaging="true" OnRowUpdating="GridView1_RowUpdating" Width="800"&gt;
      &lt;Columns&gt;
          &lt;asp:TemplateField HeaderText="Employee ID"&gt;
              &lt;ItemTemplate&gt;
                  &lt;%#Eval("EmpID")%&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Employee Name"&gt;
              &lt;ItemTemplate&gt;
                  &lt;%#Eval("EmpName")%&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="txtEmpName" runat="server" Text='&lt;%#Eval("EmpName") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField HeaderText="Salary"&gt;
              &lt;ItemTemplate&gt;
                  &lt;%#Eval("EmpSalary")%&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:TextBox ID="txtEmpSalary" runat="server" Text='&lt;%#Eval("EmpSalary") %&gt;'&gt;&lt;/asp:TextBox&gt;
              &lt;/EditItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:CommandField HeaderText="Modify" ShowEditButton="true" EditText="Edit"&gt;
              &lt;ControlStyle Width="50" /&gt;
          &lt;/asp:CommandField&gt;
          &lt;asp:TemplateField HeaderText="Delete"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server" OnClientClick="return confirm('Are you sure you want to delete these records?');"&gt;Delete&lt;/asp:LinkButton&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
  
          **&lt;asp:TemplateField HeaderText="Tick to Delete Rows"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:CheckBox ID="chkDelete" runat="server" Text="Select" /&gt;
              &lt;/ItemTemplate&gt;
              &lt;HeaderTemplate&gt;
              &lt;/HeaderTemplate&gt;
          &lt;/asp:TemplateField&gt;**
  
      &lt;/Columns&gt;
  
  &lt;/asp:GridView&gt;
  
  &lt;/div&gt;
  
  &lt;p style="width: 799px"&gt;       
   &lt;asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Delete Checked items" Width="162px" /&gt;&lt;/p&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p><strong>default.aspx.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Configuration;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.HtmlControls;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Linq;
  using System.Xml.Linq;
  using System.Data;
  using System.Data.SqlClient;
  
  public partial class _Default : System.Web.UI.Page
  {
      SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["constring"]);
      SqlCommand sqlcmd = new SqlCommand();
      SqlDataAdapter da = new SqlDataAdapter();
      DataTable dt = new DataTable();
      DataTable dt1 = new DataTable();
      DataRow dr;
      DataRow dr1;
      DataSet ds = new DataSet();
  
      protected void Page_Load(object sender, EventArgs e)
      {
          Label1.Text = "";
          //lbldbmsg.Text = "";
          if (!Page.IsPostBack)
          {
              dt.Columns.Add("EmpID");
              dt.Columns.Add("EmpName");
              dt.Columns.Add("EmpSalary");
              Session["reptable"] = dt;
              GridData();            
          }
      }
  
  protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
          string EmpID;
          EmpID = GridView1.DataKeys[e.RowIndex].Value.ToString();
          if (Session["reptable"] != null)
          {
              DataTable dt1 = new DataTable();
              dt1.Clear();
              dt1 = Session["reptable"] as DataTable;
              for (int i = 0; i &lt;= dt1.Rows.Count - 1; i++)
              {
                  DataRow dr;
                  if (dt1.Rows[i][0].ToString() == EmpID)
                  {
                      dr = dt1.Rows[i];
                      dt1.Rows[i].Delete();
                      //dt1.Rows.Remove(dr);
                  }
              }
              Session.Remove("reptable");
              Session["reptable"] = dt1;
          }
          GridData();
      }
  
      protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
      {
          GridView1.PageIndex = e.NewPageIndex;
          GridData();
      }
  
  protected void Button1_Click(object sender, EventArgs e)
      {
          dt = (DataTable)Session["reptable"];
          dr = dt.NewRow();
          dr["EmpID"] = TextBox1.Text;
          dr["EmpName"] = TextBox2.Text;
          dr["EmpSalary"] = TextBox3.Text;
          dt.Rows.Add(dr);
          Session.Remove("reptable");
          Session["reptable"] = dt;
          GridData();
          TextBox1.Text = "";
          TextBox2.Text = "";
          TextBox3.Text = "";
      }
  
  protected void Button3_Click(object sender, EventArgs e)
     {
  
     }
  }
  </code></pre>