This documentation is archived and is not being maintained.

GridViewDeleteEventHandler Delegate

Represents the method that handles the RowDeleting event of a GridView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public delegate void GridViewDeleteEventHandler(
	Object sender,
	GridViewDeleteEventArgs e


Type: System.Object
The source of the event.
Type: System.Web.UI.WebControls.GridViewDeleteEventArgs
A GridViewDeleteEventArgs object that contains the event data.

The GridView control raises the RowDeleting event when a Delete button (a button with its CommandName property set to "Delete") is clicked, but before the GridView control deletes the record. This allows you to provide an event-handling method that performs a custom routine, such as canceling the delete operation, whenever this event occurs.

When you create a GridViewDeleteEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see Events and Delegates.

The following example demonstrates how to programmatically add a GridViewDeleteEventHandler delegate to the RowDeleting event of a GridView control.

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  void Page_Load(Object sender, EventArgs e)

    // Create a new GridView control.
    GridView customersGridView = new GridView();

    // Set the GridView control's properties.          
    customersGridView.ID = "CustomersGridView";
    customersGridView.DataSourceID = "CustomersSqlDataSource"; 
    customersGridView.AutoGenerateColumns = true;
    customersGridView.AutoGenerateDeleteButton = true;
    customersGridView.DataKeyNames = new String[] {"CustomerID"};

    // Programmatically register the event-handling methods.
    customersGridView.RowDeleting += new GridViewDeleteEventHandler(this.CustomersGridView_RowDeleting);

    // Add the GridView object to the Controls collection
    // of the PlaceHolder control.


  void CustomersGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)

    // User the sender parameter to retrieve the GridView control
    // that raised the event.
    GridView customersGridView = (GridView)sender;

    // Cancel the delete operation if the user attempts to remove
    // the last record from the GridView control.
    if (customersGridView.Rows.Count <= 1)

      e.Cancel = true;
      Message.Text = "You must keep at least one record.";




<html xmlns="" >
  <head runat="server">
    <title>GridViewDeleteEventHandler Example</title>
    <form id="form1" runat="server">

      <h3>GridViewDeleteEventHandler Example</h3>

      <asp:label id="Message"


      <asp:placeholder id="GridViewPlaceHolder"

      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"


The following example demonstrates how to declaratively add a GridViewDeleteEventHandler delegate to the RowDeleting event of a GridView control.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<script runat="server">

    void CustomersGridView_RowDeleting
        (Object sender, GridViewDeleteEventArgs e)
        TableCell cell = CustomersGridView.Rows[e.RowIndex].Cells[2];
        if (cell.Text == "Beaver")
            e.Cancel = true;
            Message.Text = "You cannot delete customer Beaver.";
            Message.Text = "";


<html xmlns="">
<head id="Head1" runat="server">
    <title>GridView RowDeleting Example</title>
    <form id="form1" runat="server">
        GridView RowDeleting Example
    <asp:Label ID="Message" ForeColor="Red" runat="server" />
    <br />
    <asp:GridView ID="CustomersGridView" runat="server" 
            <asp:BoundField DataField="FirstName" 
                HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="City" HeaderText="City" 
                SortExpression="City" />
            <asp:BoundField DataField="StateProvince" HeaderText="State" 
                SortExpression="StateProvince" />
    <asp:SqlDataSource ID="CustomersSqlDataSource" runat="server"
        SelectCommand="SELECT SalesLT.CustomerAddress.CustomerID, 
            FROM SalesLT.Customer 
            INNER JOIN SalesLT.CustomerAddress 
            ON SalesLT.Customer.CustomerID = 
            INNER JOIN SalesLT.Address ON SalesLT.CustomerAddress.AddressID = 
        DeleteCommand="Delete from SalesLT.CustomerAddress where CustomerID = 
            @CustomerID and AddressID = @AddressID" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorksLTConnectionString %>">
            <asp:Parameter Name="AddressID" />
            <asp:Parameter Name="CustomerID" />

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.