Wednesday, 5 March 2014

Delete multiple Rows at the same time in Gridview in asp.net

  // On button click of Delete


protected void btndelete_Click(object sender, EventArgs e)
    {
        string[] str = new string[5];
        int i=0;
        foreach (GridViewRow gr in gvDetails.Rows)
        {
            CheckBox cb = (CheckBox)gr.Cells[0].FindControl("chk");


            if (cb.Checked)
            {
               
                str[i] = Convert.ToString( cb.Text);
                i++;
              

            }


       
        }
        foreach (string s in str)
        {
           
            SqlCommand cmdd = new SqlCommand("Delete from UserInformation where UserId='"+s+"' ", con);
            con.Open();
           cmdd.ExecuteNonQuery();
            con.Close();
        }
        BindRolesDetails();
    }