Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
I am working on checkbox in GRIDVIEW. I want to delete all the selected records.. I have this code below for your reference... Please help me to make it work ... Thanks....

protected void btnDelete_Click(object sender, EventArgs e)
{
  string currentrow;
  for (int index = 0; index <= GridView1.Rows.Count - 1; index++)
  {
  CheckBox cb =  (CheckBox)GridView1.Rows[index].FindControl("chkDelete");
  if (cb.Checked)
  {
  currentrow = GridView1.DataKeys[index].Value.ToString();
  string constr = @"Data Source=OM;Initial Catalog=TestDB;Integrated    Security=True";
  string query = @"Delete FROM Categories where CategoryID='" + currentrow + "'";
  SqlConnection con = new SqlConnection(constr);
  SqlCommand cmd = new SqlCommand(query);
  SqlDataAdapter da = new SqlDataAdapter(query, constr);
  DataSet ds = new DataSet();
  da.Fill(ds, "Categories");

  GridView1.DataSource = ds.Tables["Categories"];
  GridView1.DataBind();

  }
 
  }
  }

===================================================================
For this code i am also getting an index out of range error... I am a beginner please help me find solution to this problem....Thanks


[Edited]Code is blocked in "pre" tags[/Edited]
Posted
Updated 30-Apr-11 19:55pm
v2

What you are doing is starting a for loop for all the rows that were present at the beginning.
for (int index = 0; index <= GridView1.Rows.Count - 1; index++)

And then, if you find it selected, you remove it immediately(that can be done) and then rebind the grid (this is wrong). Thus, the rows are no more selected and the count is also reduced and thus you are bound to get an error here:
CheckBox cb =  (CheckBox)GridView1.Rows[index].FindControl("chkDelete");


All you need is to remove the bind code out of For loop. Try:
protected void btnDelete_Click(object sender, EventArgs e)
{
  string currentrow;
  for (int index = 0; index <= GridView1.Rows.Count - 1; index++)
  {
       CheckBox cb =  (CheckBox)GridView1.Rows[index].FindControl("chkDelete");
       if (cb.Checked)
       {
          // Just delete the selected row and let the loop repeat. 
       }
  }
}


BTW, use Visual Studio DEBUUGER, it's a great tool. Had you used that you would have find it yourself. It was a logical mistake.
 
Share this answer
 
Comments
su_ma2010 1-May-11 2:30am    
I am a beginner and not able to get what members are saying.. Please can u`ll modify my code where necessary so that i can understand..Thanks
Sandeep Mewara 1-May-11 3:07am    
1. I already made the necessary change
2. If you are beginner as you say so, you should be trying more. I gave you much more than a direction.
3. Try to learn. As already said, you can easily trace it out by using DEBUGGER. You don't need to know any rocket science for it. Common sense it would be.

Lastly, if we offer you the exact codes, how will you learn?
su_ma2010 1-May-11 3:31am    
After trying a lot i asked my question here..If U KNOW plzz modify
Sandeep Mewara 1-May-11 14:28pm    
Haha! I did not provide you the exact code and you downvoted me. :thumbsup:

BTW, I just happen to notice that we have met before. Apologies, my mistake I replied you. :)
I will start the answer with a question.

1. Have you bind the CountryID in the GridView?
2. To what type of control it is binded ? Is it a TextBox, a Label, a DropDownList etc. If so then

C++
protected void clickMe(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        for (int index = 0; index <= GridView1.Rows.Count - 1; index++)
        {
            CheckBox cb = (CheckBox)GridView1.Rows[index].FindControl("chkDelete");
            if (cb.Checked)
            {
                Label lblCountryID = (Label)GridView1.Rows[index].FindControl("lblCountryID");// Assuming that CountryID column is binded to Label control with a name lblCountryID.
                if (!string.IsNullOrWhiteSpace(lblCountryID.Text))
                {
                    string constr = "Data Source=OM;Initial Catalog=TestDB;Integrated Security=True";
                    string nonQuery = "Delete FROM Categories where CategoryID='" + lblCountryID.Text + "'";
                    string query = "Select * Categories";
                    SqlConnection con = new SqlConnection(constr);
                    SqlCommand cmd = new SqlCommand(nonQuery, con);
                    cmd.ExecuteNonQuery();
                    SqlDataAdapter da = new SqlDataAdapter(query, con);
                    da.Fill(dt);
                }
            }
        }

        GridView1.DataSource = dt;
        GridView1.DataBind();
    }




I hope this will fix your problem.
 
Share this answer
 
v2
Hi,

To iterate GridView row items you can use this code
foreach (GridViewRow grv in Gridview1.Rows)
{
   CheckBox chkDelete = grv.FindControl("chkDelete") as CheckBox;
   if (chkDelete.Checked)
   {
     string constr = @"Data Source=OM;Initial Catalog=TestDB;Integrated    Security=True";
     string query = @"Delete FROM Categories where CategoryID='" + Gridview1.DataKeys[grv.DataItemIndex].Value.ToString() + "'";
     SqlConnection con = new SqlConnection(constr);
     SqlCommand cmd = new SqlCommand(query);
     cmd.Connection = con;
     cmd.ExecuteNonQuery();
   }
}

Hope this will help.
 
Share this answer
 
v2
Comments
su_ma2010 1-May-11 2:30am    
I am a beginner and not able to get what members are saying.. Please can u`ll modify my code where necessary so that i can understand..Thanks
Shahriar Iqbal Chowdhury/Galib 1-May-11 4:13am    
hi, I have updated the solution check now.
 
Share this answer
 
Try this
protected void btnDelete_Click(object sender, EventArgs e)
        {
            string currentrow;
            string constr = @"Data Source=OM;Initial Catalog=TestDB;Integrated    Security=True";
            string query = string.Empty;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = null;
            SqlDataAdapter da = null;
            for (int index = 0; index <= GridView1.Rows.Count - 1; index++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[index].FindControl("chkDelete");
                if (cb.Checked)
                {
                    currentrow = GridView1.DataKeys[index].Value.ToString();
                    query = @"Delete FROM Categories where CategoryID='" + currentrow + "'";
                    
                    cmd = new SqlCommand(query);
                    da = new SqlDataAdapter(query, constr);
                    cmd.ExecuteNonQuery();
                }
            }
            query = @"Select * FROM Categories";
            cmd = new SqlCommand(query);
            da = new SqlDataAdapter(query, constr);
            DataSet ds = new DataSet();
            da.Fill(ds, "Categories");
            GridView1.DataSource = ds.Tables["Categories"];
            GridView1.DataBind();
        }
 
Share this answer
 

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