title: How can i delete selected data from grid on the button click event?
body: <p>I have a grid on my page and using check box for checking one or more than one row in grid and i want to delete checked row from grid on the click of delete button which is out side the grid..</p>
  
  <p>In table i have only name field there is no field like ID.</p>
  
  <p>How can i delete record.</p>
  
  <p>Thanks in advance..</p>
  
  <p>Here is my code What i am doing:-</p>
  
  <pre><code>private void GetData()
  {
  
      ArrayList arr;
      if (ViewState["TotalRecords"] != null)
      {
          arr = (ArrayList)ViewState["TotalRecords"];
      }
      else
      {
          arr = new ArrayList();
      }
      for (int i = 0; i &lt; grdlistWord.Rows.Count; i++)
      {
  
          CheckBox chk = (CheckBox)grdlistWord.Rows[i].Cells[1].FindControl("chkWord");
          if (chk.Checked)
          {
              if (!arr.Contains(grdlistWord.Rows[i].Cells[1]))
              {
                  arr.Add(grdlistWord.Rows[i].Cells[1]);
              }
          }
          else
          {
              if (arr.Contains(grdlistWord.Rows[i].Cells[1]))
              {
                  arr.Remove(grdlistWord.Rows[i].Cells[1]);
              }
          }
  
      }
      ViewState["TotalRecords"] = arr;
  }
  
  protected void lnkbtnDelete_Click(object sender, EventArgs e)
  {
  
      try
      {
          int count = 0;
  
          ArrayList arr = (ArrayList)ViewState["TotalRecords"];
          count = arr.Count;
          for (int i = 0; i &lt; grdlistWord.Rows.Count; i++)
          {
              if (arr.Contains(grdlistWord.Rows[i].Cells[1].Text))
              {
                  Response.Write(grdlistWord.Rows[i].Cells[1].Text.ToString());
                  DeleteRecord(grdlistWord.Rows[i].Cells[1].Text.ToString());
                  arr.Remove(grdlistWord.Rows[i].Cells[1].Text);
              }
          }
          ViewState["TotalRecords"] = arr;
          GridBind();
  
      }
      catch (SqlException ex)
      {
          ex.ToString();
      }
  
  }
  
  
  private void DeleteRecord(string word)
  {
  
      string query = "delete from searchword where word=@word";
  
      SqlCommand cmd = new SqlCommand(query, con);
      cmd.Parameters.AddWithValue("@word", word);
      con.Open();
      cmd.ExecuteNonQuery();
      con.Close();
  }
  </code></pre>
  
  <p>gridview html detail:</p>
  
  <pre><code> &lt;fieldset&gt;
          &lt;legend&gt;List&lt;/legend&gt;
          &lt;asp:GridView ID="grdlistWord" runat="server" DataKeyNames="word" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" OnRowDataBound="grdlistWord_RowDataBound" OnRowDeleting="grdlistWord_RowDeleting"&gt;
              &lt;Columns&gt;
                  &lt;asp:TemplateField&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:CheckBox ID="chkWord" runat="server" onclick="check_click(this);" /&gt;
  
                      &lt;/ItemTemplate&gt;
                      &lt;HeaderTemplate&gt;
                          &lt;asp:CheckBox ID="chkAll" runat="server" onclick="checkAll(this);" /&gt;
                          &lt;asp:LinkButton ID="ButtonDelete" runat="server" Text="Delete" OnClick="ButtonDelete_Click"&gt;&lt;/asp:LinkButton&gt;
                      &lt;/HeaderTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:BoundField DataField="Word" HeaderText="Word" /&gt;
                  &lt;asp:HyperLinkField HeaderText="Edit" Text="edit" DataNavigateUrlFields="Word" DataNavigateUrlFormatString="SearchWord.aspx?words={0}&amp;amp;mode=Edit" /&gt;
                  &lt;asp:CommandField ShowDeleteButton="True" HeaderText="Delete" /&gt;
              &lt;/Columns&gt;
              &lt;FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
              &lt;RowStyle BackColor="#F7F6F3" ForeColor="#333333" /&gt;
              &lt;EditRowStyle BackColor="#999999" /&gt;
              &lt;SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /&gt;
              &lt;PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /&gt;
              &lt;HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /&gt;
              &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775" /&gt;
              &lt;EmptyDataTemplate&gt;Records not exist!&lt;/EmptyDataTemplate&gt;
          &lt;/asp:GridView&gt;
  
          &lt;asp:HiddenField ID="hfCount" runat="server" Value = "0" /&gt;
  
      &lt;/fieldset&gt;
  </code></pre>