Editar

Compartilhar via


GridView.AutoGenerateEditButton Property

Definition

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

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

Property Value

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

Examples

The following example demonstrates how to use the AutoGenerateEditButton property to enable the automatic editing feature of the 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 updating is bound to a GridView control, the GridView control can take advantage of the data source control's capabilities and provide automatic updating functionality.

Note

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

When the AutoGenerateEditButton property is set to true, a column (represented by a CommandField object) with an Edit button for each data row is automatically added to the GridView control. Clicking an Edit button for a row puts that row in edit mode. When a row is in edit mode, each column field in the row that is not read-only displays the appropriate input control, such as a TextBox control, for the field's data type. This allows the user to modify the field's value.

When clicked, the Edit button is also replaced with an Update button and a Cancel button. Clicking the Update button updates the row in the data source with any value changes and returns the row to display mode. Clicking the Cancel button abandons any value changes and returns the row to display mode.

Note

You can programmatically put a row in edit mode by setting the EditIndex property with the index of the row. To programmatically exit edit mode, set the EditIndex property to -1.

When using the built-in updating functionality, you must set the DataKeyNames property with a comma-separated list of field names to identify the primary key field or fields of the data source; otherwise, the built-in updating functionality will not be able to update the correct record. When using automatically generated field columns (by setting the AutoGenerateColumns property to true), the GridView control automatically ensures that the automatically generated field columns that correspond to the field or fields specified in the DataKeyNames property are read-only.

You can control the appearance of a row that is in edit mode by using the EditRowStyle property. Common settings usually include a custom background color, foreground color, and font properties.

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

Event Description
RowCancelingEdit Occurs when a row's Cancel button is clicked, but before the GridView control cancels out of edit mode. This event is often used to stop the canceling operation.
RowEditing Occurs when a row's Edit button is clicked, but before the GridView control enters edit mode. This event is often used to cancel the editing operation.
RowUpdated Occurs when a row's Update button is clicked, but after the GridView control updates the row. This event is often used to check the results of the update operation.
RowUpdating Occurs when a row's Update button is clicked, but before the GridView control updates the row. This event is often used to cancel the updating operation.

Applies to

See also