Jeano Ermitaño
Jeano Ermitaño

Reputation: 179

Delete Selected Dynamic Checkboxes on Gridview ASP.NET

I have a gridview that contains data from a database. I have dynamically created checkboxes for each row. What I want is when I click the "delete selected" button, the checkboxes that are checked will be deleted. But the rows don't get deleted when I click the button. Here is the code for the button:

protected void btnDeleteSelectedServiceProvidersLocations_Click(object sender, EventArgs e)
    {
        int x = 102;
        string delete;
        foreach (GridViewRow grow in gvServiceProviders.Rows)
        {
            delete = Request.Form["gvServiceProviders$ct" + x + "$cbSelect"];
            if (delete != null || delete == "on" || delete == "y")
            {
                bll.ServiceProviderLocationID = grow.Cells[1].Text;
                bll.IsDeleted = "y";
                bll.ServiceProviderLocationDelete();
            }
            x++;
        }
        gvServiceProviders.DataSource = bll.GetServiceProviderLocations();
        gvServiceProviders.DataBind();
    }

The gridview is inside an update panel, if that helps. And I'm using a three-tier approach.

ASPX code:

<div ID="gridView">
     <asp:GridView ID="gvServiceProviders" runat="server">
            <Columns>
                  <asp:templatefield HeaderText="Select">
                       <itemtemplate>
                                <asp:CheckBox ID="cbSelect" runat="server"/>
                       </itemtemplate>
                  </asp:templatefield>
            </Columns>
     </asp:GridView>
 </div>

Upvotes: 0

Views: 1882

Answers (2)

Mahesh
Mahesh

Reputation: 8892

You want to delete the data when the check box is selected then you can do as below,

   foreach (GridViewRow grow in gvServiceProviders.Rows)
    {
       //Make sure it is datarow
       if(grow.RowType = RowType.DataControlRowType.DataRow)
       {
          //Finiding checkbox control in gridview for particular row
          CheckBox chkdelete = (CheckBox)grow.FindControl("cbSelect");

          //Make sure if checkbox is selected for the processing row
          if(chkdelete.Checked)
          {
            //Getting your datakey value
            bll.ServiceProviderLocationID = Convert.ToInt32(gvServiceProviders.DataKeys[grow.RowIndex].Value);
            bll.IsDeleted = "y";
            bll.ServiceProviderLocationDelete(); 
          }             
       }
    }
    gvServiceProviders.DataSource = bll.GetServiceProviderLocations();
    gvServiceProviders.DataBind();

As this is using the datakey of the gridview you need to set the DataKey property of the gridview with the LocationID.

Upvotes: 2

vignesh
vignesh

Reputation: 97

Try this code:
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in gvDetails.Rows)
{
//Finiding checkbox control in gridview for particular row
CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkSelect");
//Condition to check checkbox selected or not
if (chkdelete.Checked)
{
//Getting UserId of particular row using datakey value
int usrid = Convert.ToInt32(gvDetails.DataKeys[gvrow.RowIndex].Value);
using (SqlConnection con = new SqlConnection("Data Source=Test;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("delete from UserDetails where UserId=" + usrid, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
}

Upvotes: 1

Related Questions