当我没有checkbox
在我的选择gridview
并按下删除按钮时,它显示错误“没有选中复选框”但是当我选择checkbox
并单击删除按钮后它仍然显示该错误不知道为什么?我没有使用任何数据库,我只是使用datatable
and gridview
。
这是我在删除按钮上的代码
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]);
}
}