Click here to Skip to main content
14,694,553 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
in my project i have one gridview with checkbox and one button to delete the selected row from database.What im telling is deleteting selected items from database(ex:gmail mail delete option).i tried some codes in google but its not working if u plz tell how to do step by step means its help full for me thanku.
Posted

Hi ,
Try this
           <div>
           <asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
        DataKeyNames="id" DataSourceID="SqlDataSource1"
        onrowdeleting="GridView1_RowDeleting">
        <columns>
            <asp:boundfield datafield="id" headertext="id" insertvisible="False">
                ReadOnly="True" SortExpression="id" />
            <asp:boundfield datafield="cate" headertext="cate" sortexpression="cate" />
            <asp:templatefield>
                <itemtemplate>
                    <asp:checkbox id="CheckBox2" runat="server">
                        oncheckedchanged="CheckBox2_CheckedChanged" />
                </asp:checkbox></itemtemplate>
            </asp:templatefield>
            <asp:commandfield showdeletebutton="True" />
        </asp:boundfield></columns>
    </asp:gridview>
    <asp:sqldatasource id="SqlDataSource1" runat="server" xmlns:asp="#unknown">
        ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
        SelectCommand="SELECT * FROM [cate]"></asp:sqldatasource>
           <br />
           <asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" xmlns:asp="#unknown">
                   style="height: 26px" />
</asp:button></div>


  protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (((CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBox2")) is CheckBox)
          
        {
            if (((CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBox2")).Checked == true)
            { 
            //delete from DB
            }
        }
    }

protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
    if (((CheckBox)sender).Checked == true )
    {
       //Do Somthing
    }
    else if (((CheckBox)sender).Checked == false)
    {
      //do something else
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row  in GridView1.Rows)
    {
        if ((CheckBox)row.FindControl("CheckBox2") is CheckBox)
        {
            if (((CheckBox)row.FindControl("CheckBox2")).Checked == true)
            {
                //Do Somthing
            }
            else if (((CheckBox)row.FindControl("CheckBox2")).Checked == false)
            {
                //do something else
            }
        }
    }
}

Best Regards
M.Mitwalli
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900