show-notice
hide-notice

Wednesday 11 September 2013

Multiple Rows Delete in a GridView in ASP.Net


Introduction:

Here i will explain  how to Multiple Rows Delete in a GridView in ASP.Net.

Description:

While fetching data from SQL tables to a webpage, adding data source to the GridView comes foremost. The next big thing is to provide an interface to the user so that data can be accessed, updated, removed in lesser clicks.The lesser the process is hectic, more user friendly our interface becomes.

Example:


 
<asp:gridview datakeynames="empid" datasourceid="SqlDataSource1" id="GridView1" runat="server">

       <columns>

           <asp:templatefield>

                <itemtemplate>

                <asp:checkbox id="chk" onclick="Check_Click(this)" runat="server">

                </asp:checkbox></itemtemplate>

            </asp:templatefield>

 <asp:boundfield datafield="empid" headertext="empid" insertvisible="False" readonly="True" sortexpression="empid">

            <asp:boundfield datafield="name" headertext="name" sortexpression="name">

            <asp:boundfield datafield="password" headertext="password" sortexpression="password">

            <asp:boundfield datafield="city" headertext="city" sortexpression="city">

            <asp:boundfield datafield="country" headertext="country" sortexpression="country">

        </asp:boundfield></asp:boundfield></asp:boundfield></asp:boundfield></asp:boundfield>
</columns>

</asp:gridview>
C#
 protected void btnDelete_Click(object sender, EventArgs e)

{

        SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=Employee;Integrated Security=True");

        con.Open();

        int count = 0;

        for (int i = 0; i < GridView1.Rows.Count; i++)

        {

        CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chk");

            if (chk.Checked)

            {

                string id = GridView1.DataKeys[i].Value.ToString();

                SqlCommand cmd = new SqlCommand("delete from users where empid='"+id+"'", con);

                cmd.ExecuteNonQuery();

                count++;

            }

        }

        Response.Write("");

        con.Close();

DataBind();

}

SHARE THIS POST   

4 comments :

  1. very informative post for me as I am always looking for new content that can help me and my knowledge grow better.

    ReplyDelete
  2. I really appreciate your skilled approach. These square measure items of terribly helpful data which will be of nice use on behalf of me in future.

    ReplyDelete
  3. Nice post, things explained in details. Thank You.

    ReplyDelete

Design by Gohilinfotech | www.gohilinfotech.blogspot.com