Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to delete grid view row by checking a checkbox on gridview by a external button:
In Grid View I have 4 Rows I checked two rows chekbox and still else block is executing and printing Hello 4 Times.
C#
int i;
        for (i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
            if (cb.Checked)
            {
                string x = "Delete from Persons where LastName='" + GridView1.Rows[i].Cells[0].Text + "'";
                SqlCommand cmd = new SqlCommand(x, con);
                cmd.ExecuteNonQuery();
            }
            else
            {
       Response.Write("hello<br>");
            }
        }

GridView Code:
XML
<asp:GridView ID="GridView1" runat="server" Height="44px" Width="292px">
        <Columns>
            <asp:TemplateField HeaderText="Delete">
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Please Help !!!
Posted
Updated 4-Oct-12 22:44pm
v2
Comments
Pr!y@ 5-Oct-12 7:06am    
i edit the code, only cb.Checked==false is executing....

if (cb.Checked==true)
{
// string x = "Delete from Persons where LastName='" + GridView1.Rows[i].Cells[0].Text + "'";
//SqlCommand cmd = new SqlCommand(x, con);
//cmd.ExecuteNonQuery();


Response.Write("cb is checked true<br>");

}

else if (cb.Checked==false)
{
Response.Write("cb checked is false<br>");
}
else

{
Response.Write("Other");
}

Your query seems to be good. There is no error in your coding. Just put a break-point in for loop, and try stepping through the codes. And check what value is coming for your CheckBox.
Otherwise try this alternative:
C#
foreach(GridViewRow row in GridView1.Rows) {
    if(row.RowType == DataControlRowType.DataRow) {
        CheckBox cb = row.FindControl("CheckBox1") as CheckBox;
        if (cb.Checked)
        {
            string x = "Delete from Persons where LastName='" + GridView1.Rows[i].Cells[0].Text + "'";
            SqlCommand cmd = new SqlCommand(x, con);
            cmd.ExecuteNonQuery();
        }
        else
        {
           Response.Write("hello<br>");
        }
    }
}



--Amit
 
Share this answer
 
v2
Comments
Pr!y@ 5-Oct-12 7:05am    
i edit the code, only cb.Checked==false is executing....

if (cb.Checked==true)
{
// string x = "Delete from Persons where LastName='" + GridView1.Rows[i].Cells[0].Text + "'";
//SqlCommand cmd = new SqlCommand(x, con);
//cmd.ExecuteNonQuery();


Response.Write("cb is checked true<br>");

}

else if (cb.Checked==false)
{
Response.Write("cb checked is false<br>");
}
else

{
Response.Write("Other");
}
I was binding GridView in Page Load,

So,

The Problem Was Occurring,

Solution,


C#
protected void Page_Load(object sender, EventArgs e)
   {
       con = new SqlConnection("server=Harshit-PC; database=Temp; uid=sa;pwd=india");
       con.Open();

       if (Page.IsPostBack == false)
       {
           string x = "select * from cus";
           da = new SqlDataAdapter(x, con);
           da.Fill(ds);
           GridView1.DataSource = ds;
           GridView1.DataBind();
       }
   }



Thanks EveryOne........
 
Share this answer
 
hi, I just test this code and it prints like this

unchecked
checked
unchecked
checked
unchecked
checked 

C#
protected void btn1_Click(object sender, EventArgs e)
   {
       int i;
       for (i = 0; i < GridView1.Rows.Count; i++)
       {
           CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
           if (cb.Checked)
           {
               //string x = "Delete from Persons where LastName='" + GridView1.Rows[i].Cells[0].Text + "'";
               //SqlCommand cmd = new SqlCommand(x, con);
               //cmd.ExecuteNonQuery();
               Response.Write("checked<br>");
           }
           else
           {
               Response.Write("unchecked<br>");
           }
       }
   }</br></br>

there are 6 records loaded in the gridview and I checked its alternative rows. hence whenever it prints checked it should delete the record. rebind the gridview after the delete operation to see it result.
 
Share this answer
 
Try by setting autopostback property to true
 
Share this answer
 
Comments
Pr!y@ 5-Oct-12 7:49am    
Not Working

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