title: Delete rows in Gridview
body: <p>I am having a problem with deleting gridview rows. I am populating my gridview by selecting value in a dropdownlist then by clicking the add button, value will be added in my gridview, here is my code:</p>
  
  <p><strong>In my aspx:</strong></p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" 
              CssClass="mGrid" EmptyDataText = "There are no records to display"&gt;
              &lt;Columns&gt;
                  &lt;asp:TemplateField ItemStyle-Width="10"&gt;
          &lt;HeaderTemplate&gt;
  
          &lt;/HeaderTemplate&gt;
          &lt;ItemTemplate&gt;
              &lt;asp:CheckBox ID="CheckBox1" runat="server"/&gt;
          &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
  
              &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p><strong>In my code behind:</strong></p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
      {
          DataSet ds = new DataSet();
          DataTable dt = new DataTable();
          DataColumn dc = new DataColumn("Id");
          DataColumn dc1 = new DataColumn("Name");
          //DataColumn dc2 = new DataColumn("Id");
  
          dt.Columns.Add(dc);
          dt.Columns.Add(dc1);
         // dt.Columns.Add(dc2);
          ds.Tables.Add(dt);
          Session["data"] = ds;
          GridView1.DataBind();
      }
  }
  
  protected void btnSave_Click(object sender, EventArgs e)
  {
      SqlConnection con = new SqlConnection("Data Source=GATE-PC\\SQLEXPRESS;Initial Catalog=dbProfile;Integrated Security=True");
      con.Open();
  
      foreach (GridViewRow row in GridView1.Rows)
      {
          SqlCommand cmdd = new SqlCommand("Insert into Profile (Id, profile_Id)VALUES(@id, @pid)", con); 
          cmdd.CommandType = System.Data.CommandType.Text;
          cmdd.Parameters.AddWithValue("@id", row.Cells[1].Text);
          cmdd.Parameters.AddWithValue("@pid", txtbid.Text);
          cmdd.ExecuteNonQuery();
      }
  }
  
  protected void btnAdd_Click(object sender, EventArgs e)
  {
      DataSet ds = (DataSet)Session["data"];
      DataRow dr = ds.Tables[0].NewRow();
      dr[0] = DropDownList1.Text.Trim();
      dr[1] = DropDownList1.SelectedItem;
      //dr[2] = txtId.Text.Trim();
      ds.Tables[0].Rows.Add(dr);
      GridView1.DataSource = ds;
      GridView1.DataBind();
  }
  </code></pre>
  
  <p>Now, I am trying to remove checked rows on my gridview with this code:</p>
  
  <pre><code>protected void btnRemove_Click(object sender, EventArgs e)
  {
      ArrayList del = new ArrayList();
  
      foreach (GridViewRow row in GridView1.Rows)
      {
          if (row.RowType == DataControlRowType.DataRow)
          {
              CheckBox chkDelete = (CheckBox)row.Cells[0].FindControl("CheckBox1");
  
              if (chkDelete != null)
              {
                  if (chkDelete.Checked)
                  {
  
                      string id = row.Cells[1].Text;
                      del.Add(id);
                  }
              }
          }
      }
      GridView1.DataBind();
  }
  </code></pre>
  
  <p>With the btnRemove codes above, If I clicked it, it'll remove all values in my gridview even the unchecked rows, what I want is that just the checked rows not all. And are there any other simple way of removing rows in a gridview than using checkbox? I am using c# with asp.net. </p>