System.Web.UI.WebControls Namespace GridView Class
.NET Framework version 2.0
Sets or retrieves a value specifying whether a CommandField column with a Select button for each data row is automatically added and displayed in a GridView control.
Inline |
<asp:gridview autogenerateselectbutton = true | false ... > |
Script |
GridView.AutoGenerateSelectButton [ = true | false ] |
This property accepts or returns only a boolean value: true if a CommandField column with a Select button for each data row is automatically added and displayed; otherwise, false. Default value is false.
Use this property to automatically create a CommandField column with a Select button for each data row in a GridView control.
Clicking the Select button for a row sets the SelectedIndex property to the index of the row, and also sets a reference to the selected GridViewRow object via the SelectedRow property.
Note that for the automatic selection feature to work, the DataKeyNames property must be set to specify the key field or fields of the data source that will identify the record to select.
Optionally, the DataKeyNames property can be set to specify the key field or fields in the data source that uniquely identifies the record bound to each row. This provides a convenient way to access the GridView's SelectedValue property, which would contain the value(s) of the key field(s) when the Select button for a row is clicked.
The GridView control raises the following events to which you can assign a custom action to perform when a row is selected.
Event |
Description |
SelectedIndexChanging |
Occurs when a row's Select button is clicked, but before the GridView control handles the select operation. This event is often used to cancel the selection operation. |
SelectedIndexChanged |
Occurs when a row's Select button is clicked, but after the GridView control handles the select operation. This event is often used to perform a task after a row is selected in the control. |
The below snippet demonstrates how to use the AutoGenerateSelectButton property to enable the automatic selection feature of a GridView control.
<asp:gridview id = "empsGrid" runat="server"
datasourceid = "employees"
datakeynames = "employeeID"
autogenerateselectbutton
autogeneratecolumns=false
cellpadding=5 font-size=8pt
selectedrowstyle-backcolor = "khaki">
GridView Members AutoGenerateDeleteButton AutoGenerateEditButton Allowing Users to Select Rows in a GridView Control