1
Reply

Delete item(s) from Datagrid

Sandhya Bhosale

Sandhya Bhosale

Nov 10 2006 1:17 AM
1.9k

I am trying to delete one or more items from the datagrid using Delete button. But I receive the following error:

InvalidOperation Exception was handled by user code
Collection was modified; enumeration operation may not execute.

I have written the code as follows:


  protected void btndelete_Click(object sender, EventArgs e)
    {

               
        foreach (DataGridItem dgi in grdCorpAcc.Items)
        {
            CheckBox checkall;
            int i = 0;
            int id;

            checkall = (CheckBox)dgi.FindControl("chkSelect");

            id = (int)grdCorpAcc.DataKeys[dgi.ItemIndex];

            if (checkall.Checked == true)
            {
                string delrecord = "delete Account where Id=" +id;

                SqlConnection con = new SqlConnection(ConnectionString);

                SqlCommand cmd = new SqlCommand(delrecord, con);

                con.Open();

                cmd.ExecuteNonQuery();

                con.Close();
            }
            grdCorpAcc.CurrentPageIndex = 0;
            grdCorpAcc.EditItemIndex = -1;
            BindDataGrid();
        }

      
    }

Please help ............


Answers (1)