Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
GridView Class
 AutoGenerateDeleteButton Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
GridView..::.AutoGenerateDeleteButton Property

Gets or sets a value indicating whether a CommandField field column with a Delete button for each data row is automatically added to a GridView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
Visual Basic (Declaration)
Public Overridable Property AutoGenerateDeleteButton As Boolean
Visual Basic (Usage)
Dim instance As GridView
Dim value As Boolean

value = instance.AutoGenerateDeleteButton

instance.AutoGenerateDeleteButton = value
C#
public virtual bool AutoGenerateDeleteButton { get; set; }
Visual C++
public:
virtual property bool AutoGenerateDeleteButton {
    bool get ();
    void set (bool value);
}
JScript
public function get AutoGenerateDeleteButton () : boolean
public function set AutoGenerateDeleteButton (value : boolean)
ASP.NET
<asp:GridView AutoGenerateDeleteButton="True|False" />

Property Value

Type: System..::.Boolean
true to automatically add a CommandField field column with a Delete button for each data row; otherwise, false. The default is false.

When a data source control that supports deleting is bound to a GridView control, the GridView control can take advantage of the data source control's capabilities and provide automatic deleting functionality.

NoteNote:

For a data source control to delete data, it must be configured to delete data. To configure a data source control to delete records, see the documentation for the specific data source control.

When the AutoGenerateDeleteButton property is set to true, a column (represented by a CommandField object) with a Delete button for each data row is automatically added to the GridView control. Clicking the Delete button for a row permanently removes that record from the data source.

NoteNote:

You must also set the DataKeyNames property to identify the key field or fields of the data source for the automatic deleting feature to work.

The GridView control provides several events that you can use to perform a custom action when a row is deleted. The following table lists the available events.

Event

Description

RowDeleted

Occurs when a row's Delete button is clicked, but after the GridView control deletes the record from the data source. This event is often used to check the results of the delete operation.

RowDeleting

Occurs when a row's Delete button is clicked, but before the GridView control deletes the record from the data source. This event is often used to cancel the deleting operation.

The following example demonstrates how to use the AutoGenerateDeleteButton property to enable the automatic deleting feature of a GridView control.

Visual Basic
<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
  <head runat="server">
    <title>GridView Edit Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView Edit Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"  
        runat="server">
      </asp:gridview>

      <!-- 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]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>

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


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
  <head runat="server">
    <title>GridView Edit Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView Edit Example</h3>

      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"  
        runat="server">
      </asp:gridview>

      <!-- 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]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>

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


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker