add

Monday, 29 July 2013

Delete Multiple Records From Gridview and also asking Permission to whether the record is delete or not

First Add this script to header tag on .aspx page:

  <script type="text/javascript" language="javascript">
        function DeleteConfirmation() {
            if (confirm("Are you sure, you want to delete selected records ?") == true)
                return true;
            else
                return false;
        }
    </script>


and call this function on button like this:

OnClientClick="return DeleteConfirmation()"

First Get the records from database attache to gridview

write the following code on button click event:

 string strID = string.Empty;

        for (int i = 0; i < gvsample.Rows.Count; i++)
        {
            CheckBox chkDelete = (CheckBox)gvsample.Rows[i].
                             Cells[0].FindControl("chkSelect");
            if (chkDelete != null)
            {
                if (chkDelete.Checked)
                {

                    Label lbldeleteid = (Label)gvsample.Rows[i].Cells[1].FindControl("lblid");
                    if (strID != string.Empty)
                        strID += ",";
                    strID += "" + lbldeleteid.Text + "";
                    //strID  = lbldeleteid.Text;
                    //idCollection.Add(strID);
                }
            }
        }
        string s1 = "delete from store where id in("+strID+")";
        //sb += sb.AppendFormat("{0}='{1}'; ", s1, item);
        con.Open();
        SqlCommand cmd = new SqlCommand(s1, con);
        //cmd.ExecuteNonQuery();
        int j = Convert.ToInt32(cmd.ExecuteNonQuery());
        con.Close();
        if (j > 0)
        {
            lblsample.Text = j+"Record(s) Deleted Sucessfully";
        }
        else
        {
        }
        getdata();

No comments:

Post a Comment