When I am not selecting checkbox in my gridview and press delete button it show error “No Check box has been selected” but when after that when i select checkbox and click on delete button it still showing that error don’t knw why?
i am not using any database i just use datatable and gridview.
Here is my code on delete button
protected void DeleteButton_Click(object sender, EventArgs e)
{
var dt = (DataTable)ViewState["CurrentData"];
if (dt == null)
{
return;
}
List<DataRow> rowsToDelete = new List<DataRow>();
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if (cb != null && cb.Checked)
{
row.Visible = false;
//remove row by its index as it should GridViewRow index == DataRow index
//it is not the best way but from your code I dont have information how your GridView looks
dt.Rows.RemoveAt(row.RowIndex);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView2.DataSource = dt;
GridView2.DataBind();
ViewState["CurrentData"] = dt;
}
else
{
lblError.Visible = true;
}
for (int i = 0; i < rowsToDelete.Count; i++)
{
dt.Rows.Remove(rowsToDelete[i]);
}
}
Please run this code.