ASP.NET GridView Delete
The GridView control provides many built-in capabilities that allow the user to sort, update, delete, select, and page through items in the control. The following tutorial shows how to delete a row from Gridview and display a confirmation message before deleting the specified row.



In this article I have used SQL SERVER database for sample data.
  

    Create table  tblRecord
    (
      Id int primary key, Name varchar(200),Branch varchar(50)
     )
--Insert some record

 
Before you start to generate GridView in your asp file, you should create a ConnectionString in your web.Config File. Double click the web.config file on the right hand side of the Visual Studio and add the following connectionstring code in that file.


Web.Config File

  

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
	 <add name="dbSantoshTestConnectionString" 
         connectionString="Data Source=PS-PC\SANTOSH;Initial Catalog=dbSantoshTest;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>




To enable delete , set the AutoGenerateDeleteButton to true and specify the delete command in the SqlDataSource.

DeleteCommand="DELETE From [tblRecord] WHERE [Id] = @Id"

Here we are trying to display a confirmation message before deleting the specified row. In order to doing this we have to write a small Javascript code for display confirmation message.

function isDelete()
{
   return confirm("Do you want to delete this row ?");
 }

We have to call this isDelete() Javascript function on OnClientClick event of the delete LinkButton.

The follwoing ASP.NET program shows how to delete a row from Gridview and display a confirmation message before deleting the specified row.

Gridview.aspx

  

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Gridview Demo</title>
    <script type="text/javascript">
        function isDelete() {
            return confirm("Do you want to delete this row ?");
        }
	</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="gvDetail" runat="server" AllowSorting="True" AllowPaging="True" PageSize="3" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="sqldatasource1" CellPadding="4" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Branch" HeaderText="Branch" SortExpression="Branch" />

            <asp:TemplateField>
			<ItemTemplate>
				<asp:LinkButton ID="DeleteBtn" runat="server" CommandName="Delete"
				OnClientClick="return isDelete();">Delete
				</asp:LinkButton>
			</ItemTemplate>
		</asp:TemplateField>
        </Columns>

        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />

    </asp:GridView>


        <asp:SqlDataSource ID="sqldatasource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbSantoshTestConnectionString %>" SelectCommand="SELECT * FROM [tblRecord]" 
            DeleteCommand="DELETE From [tblRecord] WHERE [Id] = @Id">
   
    <DeleteParameters>
		<asp:Parameter Name="Id" Type="String" />
	</DeleteParameters>

        </asp:SqlDataSource>

    </div>
    </form>
</body>
</html>




Gridview.aspx.cs

  

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

public partial class Girdview : System.Web.UI.Page
{
   
    protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string cellID = e.Row.Cells[0].Text;
            LinkButton deleteButton = (LinkButton)e.Row.Cells[3].FindControl("DeleteBtn");
            if (deleteButton != null)
            {
                deleteButton.Attributes.Add("onclick", "return isDelete();");
            }
        }
    }
}