How to delete multiple rows from GridView using CheckBox in ASP.NET

Download the Source file

If you want to delete multiple rows from GridView in ASP.NET. Follow some for this task.
Step-1: Take a GridView Control and one Button control onto the Design window.
Step-2 : Set AutoGenerated column property is to false.
Step-3 : Make some columns like

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

           <asp:TemplateField HeaderText ="Id" Visible =false>
        <ItemTemplate >
            <asp:Label ID="snolbl" runat="server" Text='<%# Eval("sno") %>' />
        </ItemTemplate>
        </asp:TemplateField>

similarly bind all column onto label control. 
     
Here is the first template field take a one checkbox control and antother field take one label control.

Step-4: Bind GridView on Page_Load() event
Step-5: Find checkbox control from GridView Rows also find Unique table column (sno).
Step-6 : Perform Delete operation

Lets take a simple example
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="deleteItem.aspx.cs" Inherits="deleteItem" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false">
        <Columns>
        <asp:TemplateField HeaderText ="Action">
        <ItemTemplate >
            <asp:CheckBox ID="Chkdelete" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>


          

<asp:TemplateField HeaderText ="Id" Visible =false>
        <ItemTemplate >
            <asp:Label ID="snolbl" runat="server" Text='<%# Eval("sno") %>' />
        </ItemTemplate>
        </asp:TemplateField>



         <asp:TemplateField HeaderText ="Name">
        <ItemTemplate >
            <asp:Label ID="namelbl" runat="server" Text='<%# Eval("Name") %>' />
        </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText ="Address">
        <ItemTemplate >
      <asp:Label ID="addlbl" runat="server" Text='<%# Eval("Name") %>' />
        </ItemTemplate>
        </asp:TemplateField>
        
        </Columns>
        </asp:GridView>
   
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="Delete Multiple rows" />
   
    </div>
    </form>
</body>

</html>

Codebehid 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.SqlClient;
using System.Configuration;

public partial class deleteItem : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            loadgrid();
        }
    }

    private void loadgrid()
    {
        SqlCommand cmd = new SqlCommand("Select * from deltable",con);
        con.Open();
        SqlDataReader rd = cmd.ExecuteReader();
        GridView1.DataSource = rd;
        GridView1.DataBind();
        con.Close();

       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow  row in GridView1 .Rows)
        {
            var check = row.FindControl("Chkdelete") as CheckBox;
            if (check .Checked)
            {
                var id = row.FindControl("snolbl") as Label;
                SqlCommand cmd = new SqlCommand("delete from deltable where sno=@s", con);
                cmd.Parameters.AddWithValue("@s", id.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

               
            }
           
        }
        loadgrid();
    }
}


Output
How to delete multiple rows from GridView using CheckBox in ASP.NET
How to delete multiple rows from GridView using CheckBox in ASP.NET
How to delete multiple rows from GridView using CheckBox in ASP.NET



1 comment: