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)
{
}
}
}
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.