Delete Multiple Rows in GridView With CheckBox Selection With Confirmation in Asp.Net C#

Delete Multiple Rows in GridView With CheckBox Selection

Delete Gridview Multiple Rows Checkbox Selection at Time in ForEach Control Using Asp.Net C#




                                  Download Coding

                                                 Download


                                    DEMO





HTML CODING


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
     <table><tr><td></td><td>   <asp:GridView ID="GridView1" runat="server"
         AutoGenerateColumns="False">
            <AlternatingRowStyle BackColor="#FFCCFF" />
            <Columns>
                <asp:TemplateField HeaderText="Action">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkSelect" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:Label ID="lblID" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("city") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("country") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle BackColor="#FF3300" Height="40px" HorizontalAlign="Right" />
            <RowStyle BackColor="#CCCCFF" Height="60px" Width="80px" />
        </asp:GridView></td></tr></table>
        <br />
       
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
            Text="DELETE" OnClientClick="return  confirm(&quot;Are You Sure to Delete?&quot;)" />   
      
    </div>
    </form>
</body>
</html>



C# CODING


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.Data;

public partial class MultipleRowDeleteGridView : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    SqlDataReader rd;
    DataSet ds;
    string query;

    public void dbcon()
    {

  string connn = (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con = new SqlConnection(connn);
        con.Open();


    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            bind1();

        }
    }

    protected void bind1()
    {
        dbcon();
        query = "select * from grid";
        cmd = new SqlCommand(query, con);
        adp = new SqlDataAdapter(cmd);
        ds = new DataSet();
        adp.Fill(ds);
        rd = cmd.ExecuteReader();
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();

        }


        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columncount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            // GridView1.FooterRow.Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
            GridView1.Rows[0].Cells[0].Text = "No Records Found";
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        foreach (GridViewRow Grow in GridView1.Rows)
        {

            CheckBox chselect = (CheckBox)Grow.FindControl("chkSelect");

            Label lblId = (Label)Grow.FindControl("lblID");


            if (chselect.Checked)
            {

                dbcon();
                query = "delete from grid where  id='" + lblId.Text + "'";
                cmd = new SqlCommand(query, con);
                cmd.ExecuteNonQuery();
                Response.Write("<script>alert('Selected Row Deleted')</script>");


            }

        }

        bind1();
    }
}




First - Create Table & Table Fields Required 







Next - Table Fields Insert  the Values  for Required










Next - New Form Add - Add the Gridview From Toolbox  & Button   - Select GridView  - Right Click - Edit Column Select 











Add the Template Fields   - Change the Header   For Required Fields










Change All Template Fields  Names Required











Next - Right Click GridView -  Select Edit Templates 












Next - Add CheckBox  - Change Id For Required 












Next - Add Label -  Bind the Table Fields Eval("id") - Change the Label Id 










Next - Add Label -  Bind the Table Fields Eval("city") 









Next - Add Label -  Bind the Table Fields Eval("country") 











Next - Code Behind Page   - Add the NameSpaces - DataBase Connection  & Select database Field Inserted Values To Bind Gridview











Next - Delete Button OnClickMode - Write Delete Coding Where Condition  Using ID - Foreach Using Multiple Row Delete  at a  Time











Next - Delete Button - Property - Go To - OnClientClick  - Write the PopUp  Message  Before Delete Confirm.










Next - GridView - Header Row Color - Row Color - Alternate Row Color Changed 











Next - Run the [F5] Program -  Select - CheckBox - Delete 















1 comments:

Post a Comment