I am trying to delete selected rows from gridview in button click event but I am getting the below error:
Collection was modified; enumeration operation might not execute.
Code:
protected void btnRemoveUser_Click(object sender, EventArgs e)
{
DataTable yourDataTable = GetDataTable(Studentlist);
//CheckBox chkbx;
foreach(GridViewRow oneRow in Studentlist.Rows)
{
CheckBox chkbx = (CheckBox)oneRow.FindControl("ckbxID");
foreach (DataRow rowDT in yourDataTable.Rows) **//In this line throwing the exception**
{
if (chkbx.Checked == true)
{
yourDataTable.Rows.Remove(rowDT);
}
}
}
Studentlist.DataSource = yourDataTable;
Studentlist.DataBind();
}
Note: Studentlist is the gridview ID.