Click here to Skip to main content
12,076,586 members (44,278 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Gridview checkbox
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.
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:
<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 4-Oct-12 23:43pm
Pr!y@1.2K
Edited 4-Oct-12 23:44pm
JF201554.3K
v2
Comments
Harshit Raj Singh 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");
}
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:
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
  Permalink  
v2
Comments
Harshit Raj Singh 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");
}
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

I was binding GridView in Page Load,

So,

The Problem Was Occurring,

Solution,


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........
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

hi, I just test this code and it prints like this

unchecked
checked
unchecked
checked
unchecked
checked 
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
");
           }
           else
           {
               Response.Write("unchecked
");
           }
       }
   }</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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Try by setting autopostback property to true
  Permalink  
Comments
Harshit Raj Singh 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100