Delete Multiple Rows In GridView In Asp.Net Using Checkbox

Delete Multiple Rows In GridView In Asp.Net Using Checkbox

In this post we will discuss How to Delete Multiple Rows In GridView In Asp.Net Using Checkbox. Deletion of multiple rows (records) in GridView based on CheckBox selection.

Design:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Delete Multiple Rows In GridView In Asp.Net Using Checkbox</title>
<style type="text/css">
body
 {
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
 }
</style>
</head>
<body>
<form id="form1" runat="server">
<h2>Delete Multiple Rows In GridView In Asp.Net Using Checkbox</h2>
<div>
<asp:GridView ID="gv" runat="server" Width="100%" AutoGenerateColumns="false">
<Columns>

<asp:TemplateField HeaderText="Action">
<ItemTemplate><asp:CheckBox ID="chk" runat="server" /></ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="UID">
<ItemTemplate><asp:Label ID="uidn" runat="server" Text='<%#Eval("uid") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="UserName">
<ItemTemplate><asp:Label ID="uname" runat="server" Text='<%#Eval("username") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="User Email">
<ItemTemplate><asp:Label ID="uemail" runat="server" Text='<%#Eval("uemail") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>    

<br /><br />
<asp:Button ID="btndelete" runat="server" Text="Delete Selected Rows" OnClick="btndelete_Click" />
</div>
</form>
</body>
</html>

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            binddata();
        }
    }
    public void binddata()
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=dev;User ID=sa;Password=hightech");
        con.Open();
        string str = "select * from users";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader reader;
        reader = cmd.ExecuteReader();
        gv.DataSource = reader;
        gv.DataBind();
        con.Close();
    }
    protected void btndelete_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=dev;User ID=sa;Password=hightech");
        foreach (GridViewRow  row in gv .Rows)
        {
            var check = row.FindControl("chk") as CheckBox;
            if (check .Checked)
            {
                var id = row.FindControl("uidn") as Label;
                SqlCommand cmd = new SqlCommand("delete from users where uid=@u", con);
                cmd.Parameters.AddWithValue("@u", id.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();     
            } 
        }
        binddata();
    }
}

demo