แก้ไข

แชร์ผ่าน


GridView.AutoGenerateDeleteButton Property

Definition

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.

public:
 virtual property bool AutoGenerateDeleteButton { bool get(); void set(bool value); };
public virtual bool AutoGenerateDeleteButton { get; set; }
member this.AutoGenerateDeleteButton : bool with get, set
Public Overridable Property AutoGenerateDeleteButton As Boolean

Property Value

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

Examples

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

<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="AdventureWorksLTDataClassesDataContext"
    EnableDelete="True" EnableInsert="True" EnableUpdate="True" 
    TableName="SalesOrderDetails">
</asp:LinqDataSource>

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" 
    DataKeyNames="SalesOrderID,SalesOrderDetailID"
    DataSourceID="LinqDataSource1">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" 
            ShowEditButton="True" />
        <asp:BoundField DataField="SalesOrderID" 
            HeaderText="SalesOrderID" ReadOnly="True"
            SortExpression="SalesOrderID" />
        <asp:BoundField DataField="SalesOrderDetailID" 
            HeaderText="SalesOrderDetailID" InsertVisible="False"
            ReadOnly="True" SortExpression="SalesOrderDetailID" />
        <asp:BoundField DataField="OrderQty" 
            HeaderText="OrderQty" SortExpression="OrderQty" />
        <asp:BoundField DataField="ProductID" 
            HeaderText="ProductID" SortExpression="ProductID" />
        <asp:BoundField DataField="UnitPrice" 
            HeaderText="UnitPrice" SortExpression="UnitPrice" />
        <asp:BoundField DataField="ModifiedDate" 
            HeaderText="ModifiedDate" SortExpression="ModifiedDate" />
    </Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="AdventureWorksLTDataClassesDataContext"
    EnableDelete="True" EnableInsert="True" EnableUpdate="True" 
    TableName="SalesOrderDetails">
</asp:LinqDataSource>

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" 
    DataKeyNames="SalesOrderID,SalesOrderDetailID"
    DataSourceID="LinqDataSource1">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" 
            ShowEditButton="True" />
        <asp:BoundField DataField="SalesOrderID" 
            HeaderText="SalesOrderID" ReadOnly="True"
            SortExpression="SalesOrderID" />
        <asp:BoundField DataField="SalesOrderDetailID" 
            HeaderText="SalesOrderDetailID" InsertVisible="False"
            ReadOnly="True" SortExpression="SalesOrderDetailID" />
        <asp:BoundField DataField="OrderQty" 
            HeaderText="OrderQty" SortExpression="OrderQty" />
        <asp:BoundField DataField="ProductID" 
            HeaderText="ProductID" SortExpression="ProductID" />
        <asp:BoundField DataField="UnitPrice" 
            HeaderText="UnitPrice" SortExpression="UnitPrice" />
        <asp:BoundField DataField="ModifiedDate" 
            HeaderText="ModifiedDate" SortExpression="ModifiedDate" />
    </Columns>
</asp:GridView>

Remarks

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.

Note

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.

Note

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.

Applies to

See also