Delete GridView Selected Row Record through Button


delete button in gridview asp.net c#

In this articles i'm going to show you how to delete gridview seleted row by using CheckBox Control and Button Control. In gridview checkbox control is used to select multiple rows for deleting records.

Add GridView | Buttton | Lable : Design Page


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

<!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>
     <table style="border-collapse:collapse;">
       <tr>
        <th colspan="3" style="padding:10px 15px 10px 15px; border:1px solid #dddddd; background-color:Purple; color:#ffffff; text-align:left;">Delete Gridview Selected Row from Outsite Button </th>
       </tr>
       <tr>
        <td colspan="3" style="padding:10px 15px 10px 15px; border:1px solid #dddddd; height:150px;">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" 
                CellPadding="4">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox12" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Emp ID">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bindundefined"empid") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bindundefined"empid") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Emp Name">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bindundefined"empname") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bindundefined"empname") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Emp Salary">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bindundefined"empsal") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bindundefined"empsal") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                <RowStyle BackColor="White" ForeColor="#330099" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                <SortedAscendingCellStyle BackColor="#FEFCEB" />
                <SortedAscendingHeaderStyle BackColor="#AF0101" />
                <SortedDescendingCellStyle BackColor="#F6F0C0" />
                <SortedDescendingHeaderStyle BackColor="#7E0000" />
            </asp:GridView>
        </td>
       </tr>
       <tr>
       <td colspan="3" style="padding:10px 15px 10px 15px; border:1px solid #dddddd;">
        <asp:Button ID="btn_Delete" Text="Delete" runat="server" 
               onclick="btn_Delete_Click" />
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="lblerror" runat="server"></asp:Label>
       </td>
       </tr>
     </table>
    </div>
    </form>
</body>
</html>

C# Coding

C# Coding : Namespace


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;

C# Coding : Page Load Event

public partial class DeleteGridviewSelectedRowFromOutsideButton : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=.; User id=sa; password=tiger; Database=master;");

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

C# Coding : Bind Gridview

    private void FillGridview()
    {        
        SqlDataAdapter da=new SqlDataAdapter("Select * from emp", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

C# Coding : Delete Button Click Event

    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (((CheckBox)row.FindControl("CheckBox12")).Checked)
            {
                string empid = ((Label)row.FindControl("Label1")).Text.Trim();
                lblerror.Text = empid;
                con.Open();
                SqlCommand cmd = new SqlCommand("Delete emp where empid='" + empid + "'", con);
                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    lblerror.Text = "Deleted Employee : " + empid + " Successfully...";
                }
                con.Close();
                FillGridview();
            }
        }
    }
}
0 Komentar untuk "Delete GridView Selected Row Record through Button"

Back To Top